Forums / Developer / Create new objects/nodes through PHP

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,

eZ debug

Timing: Jan 18 2025 02:21:32
Script start
Timing: Jan 18 2025 02:21:32
Module start 'content'
Timing: Jan 18 2025 02:21:33
Module end 'content'
Timing: Jan 18 2025 02:21:33
Script end

Main resources:

Total runtime0.9954 sec
Peak memory usage4,096.0000 KB
Database Queries199

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0059 587.7344180.8281
Module start 'content' 0.00590.8700 768.5625634.1875
Module end 'content' 0.87590.1195 1,402.7500341.3672
Script end 0.9954  1,744.1172 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00370.3733210.0002
Check MTime0.00150.1482210.0001
Mysql Total
Database connection0.00070.065710.0007
Mysqli_queries0.911891.60081990.0046
Looping result0.00180.18101970.0000
Template Total0.972097.620.4860
Template load0.00210.210620.0010
Template processing0.969997.435420.4850
Template load and register function0.00020.022210.0002
states
state_id_array0.00110.106810.0011
state_identifier_array0.00090.093520.0005
Override
Cache load0.00210.20951190.0000
Sytem overhead
Fetch class attribute can translate value0.00110.107440.0003
Fetch class attribute name0.00110.106560.0002
XML
Image XML parsing0.00140.138640.0003
class_abstraction
Instantiating content class attribute0.00000.001260.0000
General
dbfile0.00150.1537320.0000
String conversion0.00000.000630.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 34
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs