Author Topic

H-Works Agency

Thursday 13 January 2011 3:44:15 am

Comment65033

Sorry in your case its the fromString() method.

When you want to update a Exponential 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 Exponential 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