eZ Community » Forums » Developer » Creating Object Relations using PHP
expandshrink

Creating Object Relations using PHP

Creating Object Relations using PHP

Thursday 13 January 2011 12:02:43 pm - 5 replies

So here is the situation:

I have an object 'editObject' with an attribute called 'other_items', which is a multiple relations attribute. I also have another object 'destObject' in hand which I would like to add to the 'other_items' as a related object to the 'editObject' object. Puff... Hope this makes sense. I will also try to show this in a snippet:

<?php
// I have these objects:
$destObject = eZContentObject::fetchByNodeID($node_id);
$editObject = eZContentObject::fetchByNodeID($item->NodeID);
 
// And an attribute:
$dataMap = $editObject->attribute('data_map');
$eObjectAtt = $dataMap['other_items'];
// The 'other_items' is a multiple related objects attribute
 
// I want to add $destObject to $editObject attribute $eObjectAtt as a related object 
?>

Tried google, the community portal, basically everything and haven't found any script or snippet which would work. I figured out how to delete the things though. But no other information on how to add things, especially with other objects already being in the list.

Thanks in advance.

Cheers.

Thursday 13 January 2011 12:14:58 pm

toString() method with concatenated destination object ids should work.

Thursday 13 January 2011 12:21:34 pm

toString() method with concatenated destination object ids should work.

Could you expand on that?

Thursday 13 January 2011 12:44:15 pm

Sorry in your case its the fromString() method.

When you want to update a ezpublish object in php you use the ezp API.

There is a very handy method called fromString() that take different argument syntax depending on the attribute datatype you are updating.

This method support majors ezpublish built-in datatype including "object relations".

Here is a code i use to loop attributes and update the object with a given data argument :

function update ($contentObjectID, $arrayDatas) {
 
// Fetch object to update
 
$contentObject = eZContentObject::fetch($contentObjectID);
 
// Get last version of the object
 
$version = $contentObject->version($contentObject->attribute('current_version'));
 
// Get all the object attributes
 
$contentObjectAttributes = $version->contentObjectAttributes();
 
// Loop all attributes of the object's class 
 
foreach(array_keys($contentObjectAttributes) as $key)
 
{
 
// Identify each attribute name
 
$contentObjectAttribute = $contentObjectAttributes[$key];
 
$contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
 
$attributeIdentifier = $contentClassAttribute->attribute("identifier");
 
 
// Get the value of the attribute
 
$value_old = $contentObjectAttribute->toString();
 
 
// Get the value of the field from function argument
 
$value_new = array_key_exists($attributeIdentifier, $arrayDatas) ? $arrayDatas[$attributeIdentifier] : ''; 
 
 
// Update if value old and new are not empty & are different from each other
 
if (($value_old == '' || $value_old == '|') && $value_new != '') && ($value_old != $value_new))
 
{
 
$contentObjectAttribute->fromString($value_new);
 
$contentObjectAttribute->store();
 
}
}
 
return $contentObjectID;
 
}

Returning on your case argument could be :

$contentObjectID : the one you want to update

$arrayDatas in this form : array('my_object_relations_attribute_identifier' => '62,28,58,20')

Those values are objects ids of the object you want to relate to your attribute.

This should work.

Modified on Thursday 13 January 2011 12:57:20 pm by H-Works Agency

Thursday 13 January 2011 1:06:56 pm

Thank you Martin Harispuru, using your snippet I have managed to create a proper script which worked like a charm. Thank you!

Cheers.

Wednesday 14 January 2015 2:59:22 pm

If anyone finds this and is having an issue getting it working I had to pass in my $arrayDatas as 

array('my_object_relations_attribute_identifier' => '62-28-58-20')

With '-' instead of ','

I'm running 4.7 FYI

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from