Forums / Developer / Problem with adding an object in a script program

Problem with adding an object in a script program

Author Message

Christophe Saint-Pierre

Thursday 02 October 2008 5:19:39 am

Hi,
I want to add news by a script program.
I can create the object , the attributes but the node is not created in the table ezcontentobject_tree.
The problem is when i want to store the node :

$nodeAssignment =& eZNodeAssignment::create(array(
            'contentobject_id' => $contentObject->attribute('id'),
            'contentobject_version' => $contentObject->attribute('current_version'),
            'parent_node' => $parentContentObjectTreeNode->attribute('node_id'),
            //'parent_node_id' => $parentContentObjectTreeNode->attribute('node_id'),
            'main_node_id' => 1
            //'op_code' => eZNodeAssignment::OP_CODE_CREATE
         )
);
 
$nodeAssignment->store(); 

I made some trace in the class ezpersistentobject and the sql command for the insert is strange ...
First the table is not ezcontentobject_tree but eznode_asignement wich is not a table.
And the column name doesn't match with the column of ezcontentobject_tree.
It's parent_node at the place of parent_node_id ....

PHP 5.2.0
EZPublish 4.0.0

Some ideas or better : solutions ? (-;

Thanks.

André R.

Thursday 02 October 2008 9:33:11 am

Here is an example used on a 4.0 site, hope it helps:

$newNodeAssignment = eZNodeAssignment::create( array( 
        'contentobject_id' => $newObjectID,
        'contentobject_version' => $newObject->attribute( 'current_version' ),
        'parent_node' => $node->attribute('node_id'),
        'is_main' => 1 ) 
);
$newNodeAssignment->store();

// later in the code when the object is stored as well
eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $newObjectID, 'version' => 1 ) );

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

Christophe Saint-Pierre

Friday 03 October 2008 12:50:35 am

I already have this instruction :
// later in the code when the object is stored as well
eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $newObjectID, 'version' => 1 ) );

The problem is in the $nodeAssignment->store(); instruction.
It generate a bad sql command.
I know it must be something wrong what i did because i guess this function works well ! ...

Here is my whole test code :
I execute php bin/php/script_de_test.php

#!/usr/bin/env php
<?php
//
// Test program
//

error_reporting( E_ALL | E_NOTICE );

// require_once( 'lib/ezdb/classes/ezdb.php' );
// require_once( 'lib/ezutils/classes/ezcli.php' );
// require_once( 'lib/ezutils/classes/ezsys.php' );
// require_once( 'kernel/classes/ezscript.php' );
// require_once( 'kernel/classes/ezclusterfilehandler.php' );


require 'autoload.php';


$cli = eZCLI::instance();
$script = eZScript::instance( array( 'description' => ( "Importation des actualités AFP sous le format newsMl\n" .                                                                                                            "./bin/php/import_news_afp.php" ),
                                    'use-session'    => false,
                                    'use-modules'    => false,
                                    'use-extensions' => false ) );

$script->startup();

$script->initialize();

// DEBUT du script
$parentNodeID = 67;
$user_id = 14;

// Creation du nouvel objet
$class = eZContentClass::fetchByIdentifier('actualite');
$parentContentObjectTreeNode = eZContentObjectTreeNode::fetch($parentNodeID);
$parentContentObject = $parentContentObjectTreeNode->attribute("object");
$sectionID = $parentContentObject->attribute('section_id');
$contentObject =& $class->instantiate($user_id, $sectionID);
echo "sectionID : $sectionID \n";    echo "id : . " . $contentObject->attribute('id') .  " \n";    echo "parentContentObjectTreeNode : . " . $parentContentObjectTreeNode->attribute('node_id').  " \n";
echo "current_version : . " . $contentObject->attribute('current_version') .  " \n";
echo "contentobject_id : . " .  $contentObject->attribute('id') .  " \n";       // Assignement de cet objet dans un noeud        $nodeAssignment = eZNodeAssignment::create(array(
                                                   'contentobject_id' => $contentObject->attribute('id'),
                                                   'contentobject_version' => $contentObject->attribute('current_version'),
                                                   'parent_node' => $parentContentObjectTreeNode->attribute('node_id'),
                                                   'is_main' => 1
                                                   //'op_code' => eZNodeAssignment::OP_CODE_CREATE
                                                )
                                           );

                                         ;
echo "aInfo name: " . $aInfo['name'] . " \n";    echo "nodeAssignment name: " . $nodeAssignment->name() . " \n";
echo "nodeAssignment ContentobjectID: " . $nodeAssignment->ContentobjectID . " \n";   
$nodeAssignment->store();

$contentObject->setAttribute('name', 'titre testtttttt yyyyyyyyyyyy');
$contentObject->store();

// Creation des attributs de l'objet
$attribs =& $contentObject->contentObjectAttributes();
$loopLength = count($attribs);

for($i=0;$i<$loopLength;$i++){

   echo "i : " . $attribs[$i]->attribute("contentclass_attribute_identifier") . "\n";
   switch($attribs[$i]->attribute("contentclass_attribute_identifier"))
   {
                                      case 'ville':
           $attribs[$i]->setAttribute('data_text','ville uuuuuuuuuuuu');
           $attribs[$i]->store();
       break;
              case 'titre':
           $attribs[$i]->setAttribute('data_text','titre uuuuuuuuuuuu');
           $attribs[$i]->store();
       break;
                 case 'new_identifier_ezstring':
           $attribs[$i]->setAttribute('data_text','aaaaaaaaaaaa');
           $attribs[$i]->store();
       break;
              case 'new_identifier_ezimage':
           $content =& $attribs[$i]->attribute('content');
                  $imageAltText="Alternate text";
                      $filename="test.gif";
                      $originalFilename="test.gif";
                      $content->initializeFromFile(    $filename,
                                           $imageAltText,
                                           $originalFilename);
                      $content->store();
           $attribs[$i]->store();                   break;
      }
}
$contentObject->setAttribute('status',EZ_VERSION_STATUS_PUBLISHED);
$contentObject->store();

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

$script->shutdown();
?>

Koutouan Emmanuel

Wednesday 08 October 2008 2:00:46 am

Hi , I have exactly the same problem with the 4.0.1 version of ezpublish.
The creation of the object works fine but the assignement of the node failed with no error message.The node just isn't create in the table object_tree...
Did you solve your problem ?
If yes can you tell me how ?
Thanks !

Christophe Saint-Pierre

Wednesday 08 October 2008 3:56:29 am

No I still have that problem ....
I tried with many combinations , with comparing with the ezcvsimport.php , I tried too with 4.0.1 version.
I know it must be something stupid but I can't find it.

André R.

Wednesday 08 October 2008 4:33:57 am

You need to use the modules:
http://ez.no/developer/forum/developer/writing_import_scrip_ez_4_problems

So you should have posted the debug output and I would have spotted it sooner (or you would have found the solution by searching for it).

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

Christophe Saint-Pierre

Wednesday 08 October 2008 4:57:38 am

Thanks Andre but I had already tried too with the modules :

$script = eZScript::instance( array( 'description' => ( "Importation des actualités AFP sous le format newsMl\n" .
"./bin/php/import_news_afp.php" ),
'use-session' => false,
'use-modules' => true,
'use-extensions' => true ) );

Same problem with the no insertion of the node.

eZ debug

Timing: Jan 18 2025 04:26:02
Script start
Timing: Jan 18 2025 04:26:02
Module start 'content'
Timing: Jan 18 2025 04:26:02
Module end 'content'
Timing: Jan 18 2025 04:26:03
Script end

Main resources:

Total runtime1.0812 sec
Peak memory usage4,096.0000 KB
Database Queries208

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0109 587.8594180.8125
Module start 'content' 0.01090.9313 768.6719651.2891
Module end 'content' 0.94220.1390 1,419.9609345.0547
Script end 1.0812  1,765.0156 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00480.4455210.0002
Check MTime0.00190.1731210.0001
Mysql Total
Database connection0.00150.140910.0015
Mysqli_queries0.987891.36002080.0047
Looping result0.00220.20582060.0000
Template Total1.034995.720.5174
Template load0.00230.214620.0012
Template processing1.032595.495120.5163
Template load and register function0.00020.014210.0002
states
state_id_array0.00140.132410.0014
state_identifier_array0.00180.170320.0009
Override
Cache load0.00200.1845530.0000
Sytem overhead
Fetch class attribute can translate value0.00270.254340.0007
Fetch class attribute name0.00100.093980.0001
XML
Image XML parsing0.00150.137140.0004
class_abstraction
Instantiating content class attribute0.00000.001890.0000
General
dbfile0.00360.3366280.0001
String conversion0.00000.000530.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
7content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
10content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
12content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
3content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 36
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs