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