Forums / Developer / Obain the NodeID if the only thing I have is the ObjectID?

Obain the NodeID if the only thing I have is the ObjectID?

Author Message

Clemens T

Friday 02 September 2005 3:33:50 am

Hey all
I'd like to know how to get the NodeID if I only have the ObjectID. I'm currently programming in PHP, so not in a template! This is because the eZCollaborationItem class doesn't hold the NodeID!

I think the problem is (partially) that I don't have the parent_id... so maybe through getting the parent_id.. I can use ::fetchNode(...)?

Any thoughts?

And another thing: when using the '->attribute( 'id' );' on an ezPublishObject, what does it return? Because sometimes the: myObject->ID works fine, but ->attribute( 'ID' ); doesn't..?

Thanks a lot!
Clemens

Roy Bøhmer

Friday 02 September 2005 3:59:50 am

IF you only have one node per object, or know you always are looking for the main node you can run this:

$contentObject =& eZContentObject::fetch($ObjectID);
$nodeID = $contentObject->mainNodeID();

Roy

Clemens T

Friday 02 September 2005 4:55:37 am

Thank you for the hint! I now have the following problem (I think). I'm editing the ezapprovecollaborationhandler.php file of the kernel classes of ezPublish. This handles the 'approve' action of an item. The object_id of this item is known (see first post), but when I execute your code, I don't get a valid $nodeID (or no NodeID at all.. when I do a print($nodeID).. nothing is printed). Any reason why this is.. maybe because the object is not yet published?

Thanks,
Clemens!

Luis Micunco

Thursday 04 December 2008 7:56:32 am

Try

$object =& eZContentObject::fetch( $objectID );
$nodeID = $object->attribute( 'main_node_id' );

André R.

Thursday 04 December 2008 9:58:36 am

Or (add the & like above if your on php 4):

$node = eZContentObjectTreeNode::findMainNode( $objectID, true );
$nodeId = $node->attribute('node_id');

Or if you don't need the node object, only node_id:

$nodeData = eZContentObjectTreeNode::fetchByContentObjectID( $objectID, false );
$nodeId = $nodeData['node_id'];

Tip Search for "n fetch" and "n find" in ezcontentobjecttreenode.php and ezcotententobject.php to see what kind of fetch functions you can use.

Tip2: To see what kind of attributes a class support(for classes that implements eZPersistantObject), see in the beginning of the class definition on the definition() function.
'fields' are attributes that are set when the object is constructed, while ''function_attributes" are attributes that are dynamically generated when you request them (they often need to fetch data from database before the return anything). One such example would be 'main_node_id' on eZContentObject.
In other words the first two examples needs two sql calls while these two needs one (I'm not saying that less is always better, cause it's not when your database starts to get a allot of traffic and you give it a complex sql instead of several simple ones, but enough about that..).

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom