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.