Forums / Developer / Creating content objects programmatically

Creating content objects programmatically

Author Message

Brian Gerspacher

Friday 18 November 2005 1:29:22 pm

Hi,

I'm having some trouble adding a new content object/node programmatically. For simplicity, let's just say that I have a user group (although I think this discussion will be the same for any class)...

$userGroup =& eZContentObject::fetch($myContentObjectId);

...and that I want to add a new user group as a child of that object. Here's my code to add a new group.

$contentClass =& eZContentClass::fetchByIdentifier($userGroup->className());
$newGroup =& eZContentObject::create('A new child group', $contentClass->attribute('id'));

This seems to work fine and I can retrieve the group by calling eZContentObject::fetchList() with the group's name ('A new child group') as a parameter. At this point, the group does not appear in the tree of user groups when I click on 'User accounts' in the admin site. So I have tried a few things to add the group as a child of the desired node. Something like this seems to deal with data in the eznodeassignment table...

$parameters = array(
  'contentobject_id' => $newGroup->attribute('id'),
  'contentobject_version' => $newGroup->attribute('current_version'),
  'parent_node' => $userGroup->attribute('main_node_id'),
  'is_main' => 1);
$nodeAssignment =& eZNodeAssignment::create($parameters);

And this seems to deal with the ezcontentobject_tree table...

$treeNode =& eZContentObjectTreeNode::create(
  $contentobject->attribute('main_node_id'),
  $newObject->attribute('id'));

By examining the DB (and changing values as an experiment), it seems that the path_string field of the ezcontentobject_tree table is very important in deciding how the tree is displayed in the admin site. Can someone describe how the ezcontentobject, ezcontentobject_tree, and eznodeassignment tables are related and what code should be used to manipulate them appropriately?

Thanks in advance,

Brian.

Kristof Coomans

Saturday 19 November 2005 12:59:06 am

Maybe this can help you:
http://ez.no/community/forum/developer/creating_a_simple_content_object_via_php

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

Brian Gerspacher

Monday 21 November 2005 7:29:11 am

Yes, thanks, that got me going. I can create and publish new content items. However my overall understanding of content items/versions/nodes remains blurry. I'll just take things one step at a time. (My next task is to add a location for a content item but I'll start a new thread for that.)