Forums / General / Fetching Object Relation Node Name

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

eZ debug

Timing: Jan 18 2025 11:31:48
Script start
Timing: Jan 18 2025 11:31:48
Module start 'content'
Timing: Jan 18 2025 11:31:50
Module end 'content'
Timing: Jan 18 2025 11:31:50
Script end

Main resources:

Total runtime1.8116 sec
Peak memory usage4,096.0000 KB
Database Queries210

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0063 588.8281180.8359
Module start 'content' 0.00631.5708 769.6641688.3516
Module end 'content' 1.57710.2344 1,458.0156345.0625
Script end 1.8115  1,803.0781 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00410.2288210.0002
Check MTime0.00160.0862210.0001
Mysql Total
Database connection0.00080.042510.0008
Mysqli_queries1.715194.67482100.0082
Looping result0.00290.15992080.0000
Template Total1.780498.320.8902
Template load0.00230.127520.0012
Template processing1.778098.149120.8890
Template load and register function0.00030.014610.0003
states
state_id_array0.00090.047910.0009
state_identifier_array0.00220.121820.0011
Override
Cache load0.00250.1387430.0001
Sytem overhead
Fetch class attribute can translate value0.00210.117550.0004
Fetch class attribute name0.00220.1226100.0002
XML
Image XML parsing0.00230.126450.0005
class_abstraction
Instantiating content class attribute0.00000.0015110.0000
General
dbfile0.00490.2684390.0001
String conversion0.00000.000330.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
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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/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