Forums / Developer / Creating Object Relations using PHP

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.

eZ debug

Timing: Jan 18 2025 02:56:21
Script start
Timing: Jan 18 2025 02:56:21
Module start 'content'
Timing: Jan 18 2025 02:56:21
Module end 'content'
Timing: Jan 18 2025 02:56:21
Script end

Main resources:

Total runtime0.6660 sec
Peak memory usage4,096.0000 KB
Database Queries200

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0047 587.7344180.8359
Module start 'content' 0.00470.5533 768.5703566.6250
Module end 'content' 0.55790.1079 1,335.1953337.3672
Script end 0.6659  1,672.5625 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00420.6350210.0002
Check MTime0.00160.2333210.0001
Mysql Total
Database connection0.00060.084510.0006
Mysqli_queries0.591588.81932000.0030
Looping result0.00180.27761980.0000
Template Total0.636995.620.3184
Template load0.00290.432920.0014
Template processing0.634095.199920.3170
Template load and register function0.00020.029110.0002
states
state_id_array0.00110.162010.0011
state_identifier_array0.00140.217120.0007
Override
Cache load0.00240.3660370.0001
Sytem overhead
Fetch class attribute can translate value0.00100.145730.0003
Fetch class attribute name0.00100.150460.0002
XML
Image XML parsing0.00100.146930.0003
class_abstraction
Instantiating content class attribute0.00000.002470.0000
General
dbfile0.00470.7045280.0002
String conversion0.00000.000830.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
5content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
9content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
1content/datatype/view/ezxmltags/emphasize.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/emphasize.tplEdit templateOverride template
2content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1content/datatype/view/ezxmltags/quote.tpldatatype/ezxmltext/quote.tplextension/ezwebin/design/ezwebin/override/templates/datatype/ezxmltext/quote.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 22
 Number of unique templates used: 8

Time used to render debug report: 0.0001 secs