Fetching Object Relation Node Name

Author Message

John Smith

Thursday 15 April 2010 5:47:16 am

I have edited user class by adding "Object Relation" attribute. It is just a link to some NODE. I know the way to fetch the name of the node linked by "object relation" in the template but struggling to fetch the name in PHP.

I have tried with the following Code:

$userObject = $user->attribute( 'contentobject' );
$userMap = $userObject->dataMap();
$userLink = $userMap['userLink']->content();

"userLink" is the name of the identifer in object relation attribute.

Can anybody help please.

Bertrand Dunogier

Thursday 15 April 2010 7:18:38 am

If you know how to get the info using the template engine, you can easily get it using PHP, using the attribute() method.

First, a small correction: your attribute will contain a link to an object, not a link to a node. That's why it's called object relation, and not node relation ;)

Now, for the actual question, the first pieces of your code are alright. Once you know what the content() method of an ezcontentobjectattribute of type 'object relation' returns, you're almost done. According to http://ez.no/doc/ez_publish/technical_manual/4_x/reference/datatypes/object_relation, in the "Raw output" chapter, .content on such an attribute returns the eZContentObject. All you need is the node for this object. The easiest way is to request the main node. Once you have it, you can easily get its name:

$userLink = $userMap['userLink']->content();
$node = $userLink->attribute( 'main_node' );
$nodeName = $node->attribute( 'name' );

Bertrand Dunogier
eZ Systems Engineering, Lyon
http://twitter.com/bdunogier
http://gplus.to/BertrandDunogier

John Smith

Thursday 15 April 2010 8:05:55 am

Thanks mate for clearing the point and sorting it out. Much appreciated...

Nicolas Pastorino

Thursday 15 April 2010 8:26:28 am

"

Thanks mate for clearing the point and sorting it out. Much appreciated...

"

Hi John,

You may want to mark the topic as "solved", by clicking the ticker right left to the topic's title.

Cheers and thanks Bertrand for your answer!

--
Nicolas Pastorino
Director Community - eZ
Member of the Community Project Board

eZ Publish Community on twitter: http://twitter.com/ezcommunity

t : http://twitter.com/jeanvoye
G+ : http://plus.tl/jeanvoye

John Smith

Thursday 15 April 2010 9:24:35 am

"

If you know how to get the info using the template engine, you can easily get it using PHP, using the attribute() method.

First, a small correction: your attribute will contain a link to an object, not a link to a node. That's why it's called object relation, and not node relation ;)

Now, for the actual question, the first pieces of your code are alright. Once you know what the content() method of an ezcontentobjectattribute of type 'object relation' returns, you're almost done. According to http://ez.no/doc/ez_publish/technical_manual/4_x/reference/datatypes/object_relation, in the "Raw output" chapter, .content on such an attribute returns the eZContentObject. All you need is the node for this object. The easiest way is to request the main node. Once you have it, you can easily get its name:

$userLink = $userMap['userLink']->content();
$node = $userLink->attribute( 'main_node' );
$nodeName = $node->attribute( 'name' );
"

Like in templates we use to print all the keys/ values with {$node|attribute(show)}. In php, I am using print_r. On your code I tried printing the value by

print_r ($userLink) but could find any key like "main_node".

Gaetano Giunta

Thursday 15 April 2010 2:06:57 pm

@John Smith: using print_r will not help you a lot with ezpersistentobjects, unfortunately. The key here is the attribute() function: sometimes it finds data stored as an element of the object, sometimes it calls another php function to retrieve the attribute value (hey, like properties in Delphi!).

You can find a function to easily map an ezpersistentobject to something more inspectable here: http://projects.ez.no/ggxmlview/news/ggxmlview_0_2_released

Principal Consultant International Business
Member of the Community Project Board

Bertrand Dunogier

Friday 16 April 2010 2:10:27 am

Hey John. A small background tip here: on the doc page for objects, like the ezcontentobject one you can use here, there is a 'Static' column. This column indicates wether the 'attribute' (in the persistent object sense) is a static one (e.g. always pre-fetched, as an object property) or a non-static, or dynamic one. main_node is dynamic, which means that calling this attribute, either through ->attribute( 'main_node' ) in PHP, or through object.main_node in a template, will trigger a method call.

You can find what method is called by looking at the persistent object definition in the eZContentObject class (kernel/classes/ezcontentobject.php), method "definition":

array(
...
"main_node" => "mainNode",
...
)

This means you could use $userLink->mainNode() instead of $userLink->attribute(' main_node' ). That is why print_r won't show you this value, and as Gaetano explains, the attribute template operator will, as it scans every attribute, dynamic and static, and show the values.

There is a long history of trying to figure out wether it is best to use attribute + the attribute name, or the matching method... the later is probably better when it comes to performances, but the difference is most likely minimal, while the first allows you to re-use your knowledge of the template engine persistent object API. I like this one better, as it improves readability, but the choice depends on you.

Parenthesis closed :-)

Bertrand Dunogier
eZ Systems Engineering, Lyon
http://twitter.com/bdunogier
http://gplus.to/BertrandDunogier

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 31 2025 00:40:05
Script start
Timing: Jan 31 2025 00:40:05
Module start 'layout'
Timing: Jan 31 2025 00:40:05
Module start 'content'
Timing: Jan 31 2025 00:40:06
Module end 'content'
Timing: Jan 31 2025 00:40:06
Script end

Main resources:

Total runtime0.6852 sec
Peak memory usage8,192.0000 KB
Database Queries73

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0055 588.1328151.2109
Module start 'layout' 0.00550.0040 739.3438220.6875
Module start 'content' 0.00960.6747 960.03134,590.7344
Module end 'content' 0.68430.0009 5,550.765619.8438
Script end 0.6851  5,570.6094 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00340.5019160.0002
Check MTime0.00130.1873160.0001
Mysql Total
Database connection0.00090.124310.0009
Mysqli_queries0.585385.4229730.0080
Looping result0.00070.1071710.0000
Template Total0.639793.420.3198
Template load0.00220.324920.0011
Template processing0.637493.029920.3187
Template load and register function0.00160.237910.0016
states
state_id_array0.00070.099410.0007
state_identifier_array0.00100.142620.0005
Override
Cache load0.00200.2906430.0000
Sytem overhead
Fetch class attribute can translate value0.00210.307640.0005
Fetch class attribute name0.00220.3283100.0002
XML
Image XML parsing0.00160.236240.0004
class_abstraction
Instantiating content class attribute0.00010.0081110.0000
General
dbfile0.00140.2097280.0001
String conversion0.00000.001140.0000
Note: percentages do not add up to 100% because some accumulators overlap

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
7content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
11content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
4content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
4content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
2content/datatype/view/ezxmltags/quote.tpldatatype/ezxmltext/quote.tplextension/ezwebin/design/ezwebin/override/templates/datatype/ezxmltext/quote.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 30
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs