Create new objects/nodes through PHP

Author Message

Håkan Bergman

Tuesday 04 May 2010 4:19:21 am

Hello,

I am using my own extension that have a module which will create objects.
Basic code:
$contentClassIdentifier = 'article';
$class = eZContentClass::fetchByIdentifier( $contentClassIdentifier );
$ParentNodeID = 2;
$node = eZContentObjectTreeNode::fetch( $ParentNodeID );
$contentClassIdentifier = 'article';
$class = eZContentClass::fetchByIdentifier( $contentClassIdentifier );
$ParentNodeID = 2;
$node = eZContentObjectTreeNode::fetch( $ParentNodeID );

I call class article (attributes) and then fetches node 2 (Home) because I want to place the new article in the Home subdir directory. I am using eznodeassignment to create the object and the object is created with default attributes of the class article. Evereything looks fine.

The only part it does not create data is in table ezcontentobject_tree, and my guess is that is why I can't see the new article in my admin interface. I need some input on how I can add an object, with custom attribute data and in table ezcontentobject_tree so I can view and edit the object in the admin interface.

{* Begin Code *}

------------------------------------------------------------------------------------

include_once( 'kernel/common/template.php' );$contentClassIdentifier = 'article';

$class = eZContentClass::fetchByIdentifier( $contentClassIdentifier );

$ParentNodeID = 2;

$node = eZContentObjectTreeNode::fetch( $ParentNodeID );
if ( is_object( $class ) ){
$contentClassID = $class->attribute( 'id' );

$parentContentObject =& $node->attribute( 'object' );

$accessResult = $parentContentObject->checkAccess( 'create', $contentClassID, $parentContentObject->attribute( 'contentclass_id' ) );

if ( $accessResult == '1' ) {

include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );

$user =& eZUser::currentUser();

$userID =& $user->attribute( 'contentobject_id' );

$sectionID = $parentContentObject->attribute( 'section_id' );

include_once( 'lib/ezdb/classes/ezdb.php' );

$db =& eZDB::instance();

$db->begin();

$contentObject =& $class->instantiate( $userID, $sectionID );

$nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $contentObject->attribute( 'id' ), 'contentobject_version' => $contentObject->attribute( 'current_version' ), 'parent_node' => $node->attribute( 'node_id' ) ) );

$nodeAssignment->store();

$db->commit();

}

}

------------------------------------------------------------------------------------

{* End Code *}

Håkan Bergman

Wednesday 05 May 2010 12:30:21 am

I tried a different code that will createandpublish:

$Params = array ();
$Params['parent_node_id'] = 92;
$Params['class_identifier'] = 'local_organisation';
$Params['creator_id'] = 14;
$Params['section_id'] = 2;

$AttributesData = array();
$AttributesData['name'] = 'My Company';
$Params['attributes'] = $attributesData;

$ContentObject = eZContentFunctions::createAndPublishObject($Params);

local_organisation is a custom class with one datatype with an identifier called "name".
The attribute have a default identifier "My Company". I have tried to manipulate the attribute data but nothing happens.
Is this a good way to create and publish an object below a parentnode?
Should I do this differently and does anyone know how I can manipulate the datatype data before it submits into the DB?
The code works very nice, but I am concerned if this is done the right way and what I should think of. My main problem right now is how to manipulate data. I have watched a few examples, but nothing works so far when trying to change the name of the attribute "name" from the default value I put in the class.

Best regards,

Håkan

Mark Simon

Wednesday 05 May 2010 1:30:07 am

This looks quite goot, but try to publish the new created object.

When creatind an Object and just storing it, You just have a draft of it.

after $nodeAssignment->store(); instert:

$contentObject->store();
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObject->attribute( 'id' ), 'version' => 1 ) );

www.all2e.com

Paul Leclercq

Wednesday 05 May 2010 7:38:12 am

There are 2 good ways of publishing new content when creating an import script:

The one you stated in your second post:

exemple:

<span>$params</span> = <a href="http://www.php.net/array" mce_href="http://www.php.net/array"><span>array</span></a><span>(</span><span>)</span>;
<span>$params</span><span>[</span><span>'parent_node_id'</span><span>]</span> = <span>52</span>; <span>// node id of /Media/Files</span>
<span>$params</span><span>[</span><span>'class_identifier'</span><span>]</span> = <span>'file'</span>;
<span>$params</span><span>[</span><span>'creator_id'</span><span>]</span> = <span>14</span>; <span>// admin</span>
<span>$params</span><span>[</span><span>'storage_dir'</span><span>]</span> = <span>'/tmp/data/'</span>; <span>// don't forget the ended /</span>
<span>$params</span><span>[</span><span>'section_id'</span><span>]</span> = <span>3</span>; <span>// section media</span>
 
<span>$attributesData</span> = <a href="http://www.php.net/array" mce_href="http://www.php.net/array"><span>array</span></a><span>(</span><span>)</span>;
<span>$attributesData</span><span>[</span><span>'name'</span><span>]</span> = <span>'My file'</span>;
<span>$attributesData</span><span>[</span><span>'file'</span><span>]</span> = <span>'my_file.txt'</span>;
 
<span>$params</span><span>[</span><span>'attributes'</span><span>]</span> = <span>$attributesData</span>;
<span>$contentObject</span> = eZContentFunctions::<span>createAndPublishObject</span><span>(</span> <span>$params</span> <span>)</span>;

But if I were you, I would base my import script on the following file you will find in your ezpublish installation under:

bin/php/ezcsvimport.php

You will find Marks method in that file, which is the standard method used, and everything you need to create setup your attributes with the value you would like.

All you will need is to setup your $nodeID value.

This is also the code they give you during eZSystem's advanced ezpublish training course:

-----------------------------------

$class = eZContentClass::fetchByIdentifier( 'article' );
$contentObject = $class->instantiate( $creator );
$contentObject->store();

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

$version = $contentObject->version( 1 );
$version->setAttribute( 'modified', eZDateTime::currentTimeStamp() );
$version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
$version->store();

$attributes = $contentObject->attribute( 'contentobject_attributes' );
while ( list( $key, $attribute ) = each( $attributes ) )
{
$dataString = $objectData[$key];
switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) )
{
case 'ezimage':
case 'ezbinaryfile':
case 'ezmedia':
{
$dataString = eZDir::path( array( $storageDir, $dataString ) );
break;
}
default:
}
$attribute->fromString( $dataString );
$attribute->store();
}

$operationResult = eZOperationHandler::execute( 'content', 'publish',
array( 'object_id' => $contentObjectID,
'version'=> 1 ) );

-----------------------------------

Well good luck with that anyway,

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 05:15:18
Script start
Timing: Jan 18 2025 05:15:18
Module start 'layout'
Timing: Jan 18 2025 05:15:18
Module start 'content'
Timing: Jan 18 2025 05:15:19
Module end 'content'
Timing: Jan 18 2025 05:15:19
Script end

Main resources:

Total runtime0.7254 sec
Peak memory usage4,096.0000 KB
Database Queries62

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0086 587.9375152.6250
Module start 'layout' 0.00860.0040 740.562539.4453
Module start 'content' 0.01260.7112 780.0078629.1094
Module end 'content' 0.72380.0016 1,409.117220.1563
Script end 0.7254  1,429.2734 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00380.5283160.0002
Check MTime0.00170.2327160.0001
Mysql Total
Database connection0.00160.224510.0016
Mysqli_queries0.631787.0847620.0102
Looping result0.00060.0882600.0000
Template Total0.682194.020.3410
Template load0.00210.283420.0010
Template processing0.680093.740420.3400
Template load and register function0.00020.031410.0002
states
state_id_array0.00270.376410.0027
state_identifier_array0.00080.114720.0004
Override
Cache load0.00200.26991190.0000
Sytem overhead
Fetch class attribute can translate value0.00090.127730.0003
Fetch class attribute name0.01281.770660.0021
XML
Image XML parsing0.00140.198430.0005
class_abstraction
Instantiating content class attribute0.00000.002160.0000
General
dbfile0.00110.1478210.0001
String conversion0.00000.001140.0000
Note: percentages do not add up to 100% because some accumulators overlap

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
4content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
15content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
10content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 34
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs