Forums / Developer / Get the new node_id

Get the new node_id

Author Message

Roy Bøhmer

Tuesday 30 August 2005 9:05:47 am

I finally managed to import data from an excel-file, and publish the content in some new objects/nodes.
But now I need to use these new nodes as parents to a lot more objects/nodes.

The code used to assign the new objects no nodes is pretty much a copy of some code found in the eZ-book:

$nodeAssignment =& eZNodeAssignment::create( array(
					'contentobject_id' => $contentObject->attribute('id'),
					'contentobject_version' => $contentObject->attribute('current_version' ),
					'parent_node' => $parentContentObjectTreeNode->attribute( 'node_id' ),
					'is_main' => 1
					));
$nodeAssignment->store();
$newNodeID=$nodeAssignment->attribute('id');

I hoped the $newNodeID would actually contain the node_id, but it don't. It's the id of the eznodeassignment-table. I tried use eZContentObjectTreeNode instead of eZNodeAssignment, but I only endend up emptying the ezcontentobjecttreenode-table :-)

So, how can I get the node_id of a newly created object, just assiged to a node?

Kristof Coomans

Tuesday 30 August 2005 9:46:57 pm

After you've published the content, you can fetch the node and node ID with the following code:

$objectID =& $contentObject->attribute( 'id' );
$parentNodeID =& $parentContentObjectTreeNode->attribute( 'node_id' );

$node =& eZContentObjectTreeNode::fetchNode( $objectID, $parentNodeID );

$nodeID =& $node->attribute( 'node_id' );

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

Roy Bøhmer

Wednesday 31 August 2005 1:08:02 pm

Thank you, Kristof!
I hoped there would be a solution where I could avoid another fetch, but your suggestion seems to be the solution.
Since I know the new nodes always will be the main nodes I might use the code

$nodeID=$contentObject->mainNodeID();

I found this function after I did the original post.

Again, thank you!

Roy

Clemens T

Thursday 01 September 2005 2:05:01 am

Thanks, I was looking for this as well!