Share » Forums » Developer » Creating Object Relations using PHP

Creating Object Relations using PHP

Creating Object Relations using PHP

Thursday 13 January 2011 3:02:43 am - 4 replies

Author Message

H-Works Agency

Thursday 13 January 2011 3:14:58 am

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

EZP is Great

Kestutis Armalis

Thursday 13 January 2011 3:21:34 am

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

Could you expand on that?

H-Works Agency

Thursday 13 January 2011 3:44:15 am

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.

EZP is Great

Kestutis Armalis

Thursday 13 January 2011 4:06:56 am

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

Cheers.

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

36 542 Users on board!

Forums menu