Creating Object Relations using PHP

Author Message

Kestutis Armalis

Thursday 13 January 2011 3:02:43 am

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.

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.

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.