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.

eZ debug

Timing: Jan 30 2025 19:47:35
Script start
Timing: Jan 30 2025 19:47:35
Module start 'layout'
Timing: Jan 30 2025 19:47:35
Module start 'content'
Timing: Jan 30 2025 19:47:35
Module end 'content'
Timing: Jan 30 2025 19:47:35
Script end

Main resources:

Total runtime0.0281 sec
Peak memory usage4,096.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0065 588.1563151.2109
Module start 'layout' 0.00650.0049 739.3672220.6953
Module start 'content' 0.01140.0150 960.06251,002.0547
Module end 'content' 0.02640.0017 1,962.117237.9766
Script end 0.0280  2,000.0938 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002910.2696140.0002
Check MTime0.00124.2046140.0001
Mysql Total
Database connection0.00113.798910.0011
Mysqli_queries0.00289.796130.0009
Looping result0.00010.202810.0001
Template Total0.00113.910.0011
Template load0.00093.164910.0009
Template processing0.00020.719710.0002
Override
Cache load0.00062.224510.0006
General
dbfile0.00030.933680.0000
String conversion0.00000.031440.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs