Forums / Developer / Fetch objects with eZ API ?

Fetch objects with eZ API ?

Author Message

Sébastien Landeau

Monday 15 May 2006 4:59:02 am

I've tried to fetch objects with API, users or treeNodes for example, but with no success.
But all fetch() methods I use return Null, or empty arrays.

Is there any initialization calls to do before ?

In my example below, the first fetch for user returns NULL.
The second fetch for COntentObjectTreeNode returns an empty array.

Any advice ?

Thank's a lot...

<pre>
<?php
include_once( "kernel/classes/ezcontentclass.php" );
include_once( "kernel/classes/ezcontentobjecttreenode.php" );
include_once( "kernel/classes/eznodeassignment.php" );
include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );

$userID = 64;
$parentNodeID = 2;

$injectUser = & eZUser::fetchByName('injector', true);
$parentTreeNode = & eZContentObjectTreeNode::fetch( $parentNodeID, true );

$parentContentObject = $parentTreeNode->attribute("object");
$sectionID = $parentContentObject->attribute("section_id");

$class = eZContentClass::fetchByIdentifier('lecture_note');
$newContentObject = & $class->instanciate( $userID, $sectionID);

$assignProps = array(
'contentobject_id' => $newContentObject->attribute('id'),
'contentobject_version' => $newContentObject->attribute('current_version'),
'parent_node' => $parentTreeNode->attribute('node_id'),
'is_main' => 1
);

$nodeAssign = & eZNodeASssignment::create( $assignProps );
$nodeAssign->store();

$newContentObject->setAttribute( 'titre', 'Testing Title');
$newContentObject->store();

?>
</pre>

Kåre Køhler Høvik

Monday 15 May 2006 6:40:27 am

Hi

Are you trying to do this in a script, Apache instance, or eZ publish module ?

Kåre Høvik

Sébastien Landeau

Monday 15 May 2006 9:41:27 am

Tried in Apache instance... as a simple script.

But, the script runs well as a cronjob (i.e. no execution errors) and try now to <b>INSERT NEW CONTENT in the CMS</b>.
Cheking the database, I can see that there is more data in the <i>ezcontentobject</i> table ; but I can't never see my changes from the Admin Interface.

I've tried to complete my script with a publish result check with this code block (found in the existing rssimport cronjob) :

<b><i>
$operationResult = eZOperationHandler::execute( 'content', 'publish', $pupProps );
echo "Publish result : " . print_r( $operationResult ) . "\n";

if ( !isset( $operationResult['status'] ) || $operationResult['status'] != EZ_MODULE_OPERATION_CONTINUE )
{
if ( isset( $operationResult['result'] ) && isset( $operationResult['result']['content'] ) )
$failReason = $operationResult['result']['content'];
else
$failReason = "unknown error";

$cli->error( "Publishing failed: $failReason" );
unset( $failReason );
}
</i></b>

The publish operation allways failed with an 'Unkown reason' :-(

Thank's for any complementary good advice !

---------------------------------------------------------------------
Here the new version of my Cronjob Script :

<i>
<?php
include_once( "kernel/classes/ezpersistentobject.php" );
include_once( "kernel/classes/ezcontentclass.php" );
include_once( "kernel/classes/ezcontentobjecttreenode.php" );
include_once( "kernel/classes/eznodeassignment.php" );
include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );

include_once("lib/ezutils/classes/ezoperationhandler.php");

// FETCH INJECTING USER
$injectUser = eZUser::fetchByName('admin', true);
$userID = $injectUser->attribute('contentobject_id');
echo "Have user : " . $userID . "\n";

// FETCH CONTENT CLASS
$noteClass = eZContentClass::fetchByIdentifier('lecture_note');
echo "Note class : " . $noteClass->attribute('name') . "\n";


// GET PARENT NODE AND SECTION
$parentNodeID = 2;
$parentTreeNode = eZContentObjectTreeNode::fetch( $parentNodeID );
echo "Parent node : " . $parentTreeNode . "\n";
$parentContentObject = $parentTreeNode->attribute('object');
$sectionID = $parentContentObject->attribute('section_id');
echo "Inserting in section ID " . $sectionID . "\n";

// INSTANCIATION
$contentObject = & $noteClass->instantiate( $userID, $sectionID );

// ASSIGNATION
$assignProps = array(
'contentobject_id' => $contentObject->attribute('id'),
'contentobject_version' => $contentObject->attribute('current_version'),
'parent_node' => $parentTreeNode->attribute('node_id'),
'is_main' => 0
);
print_r( $assignProps );
$nodeAssignment = & eZNodeAssignment::create( $assignProps );
$nodeAssignment->store();


// STORE EMPTY OBJECT
$contentObject->setAttribute('name', 'Note de lecture');
$contentObject->store();

// SET ATTRIBUTES
$attribs = & $contentObject->contentObjectAttributes();
foreach( $attribs as $anAttr )
{
$attID = $anAttr->attribute('contentclass_attribute_identifier');
echo "IDENT : " . $attID . "\n";
$anAttr->setAttribute('data_text', "ZZZZZZZZZZZZZZZZZZZZZZZZZZ");
$anAttr->store();
}
$contentObject->setAttribute('status', EZ_VERSION_STATUS_DRAFT);
$contentObject->store();

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

?>
</i>

Kristof Coomans

Monday 15 May 2006 10:54:05 pm

The 'is_main' index of your $assignProps array needs to be 1. There always has to be 1 main node assignment.

I'm not sure however if this is the (only) problem. Try again and let us know if it works now. Did you get any PHP errors/warnings?

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

Sébastien Landeau

Monday 15 May 2006 11:42:46 pm

Yes, I've tried to value 'is_main' to 1 in the $assignProps array....

But nothing better. :-(

Sébastien Landeau

Wednesday 17 May 2006 2:09:33 am

Thank's for advice !

Endly, here is a working example, this a CLI script :

<?php
/*
* Created on 16 mai 2006
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
include_once( 'lib/ezutils/classes/ezcli.php' );
include_once( 'kernel/classes/ezscript.php' );

include_once( "kernel/classes/ezpersistentobject.php" );
include_once( "kernel/classes/ezcontentclass.php" );
include_once( "kernel/classes/ezcontentobjecttreenode.php" );
include_once( "kernel/classes/eznodeassignment.php" );
include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
include_once( "lib/ezutils/classes/ezoperationhandler.php");

$cli =& eZCLI::instance();
$cli->output("Starting inject article script client");

$script =& eZScript::instance( array( 'debug-message' => 'Injection Script',
'use-session' => false,
'use-modules' => true,
'use-extensions' => true )
);

$script->startup();

$script->setUseDebugOutput( true );
$script->setAllowedDebugLevels( EZ_LEVEL_DEBUG );
$script->setUseDebugAccumulators( true );
$script->setUseDebugTimingPoints( true );

// $sys =& eZSys::instance();
$script->initialize();

$cli->output('Starting content injection...');

// $ini =& eZINI::instance();

$classRef = "article";
$parentNodeID = 2;


// FETCH INJECTING USER
// $injectUser = eZUser::fetchByName('admin');
$injectUser = eZUser::fetch( 14 );
if ( !$injectUser )
{
$cli->output("Admin user not found");
$script->shutdown(1, "Unable to fetch user");
}
$userID = $injectUser->attribute('contentobject_id');
$cli->output("Have user : " . $userID);


// FETCH CONTENT CLASS
$noteClass = eZContentClass::fetchByIdentifier( $classRef );
$cli->output( "Instanciating class : " . $noteClass->attribute('name') );



// GET PARENT NODE AND SECTION

$parentTreeNode = eZContentObjectTreeNode::fetch( $parentNodeID );
$cli->output( "Parent node path : " . $parentTreeNode->attribute('path_string') );
$cli->output( "Node node id : " . $parentTreeNode->attribute('path_identification_string') );

$parentContentObject = $parentTreeNode->attribute('object');
$sectionID = $parentContentObject->attribute('section_id');
$cli->output( "Inserting in section ID " . $sectionID );

// INSTANCIATION
$contentObject = & $noteClass->instantiate( $userID, $sectionID );

// ASSIGNATION
$assignProps = array(
'contentobject_id' => $contentObject->attribute('id'),
'contentobject_version' => $contentObject->attribute('current_version'),
'parent_node' => $parentTreeNode->attribute('node_id'),
'is_main' => 1
);
$cli->output("Assignement :" . print_r($assignProps) );
$nodeAssignment = & eZNodeAssignment::create( $assignProps );
$nodeAssignment->store();


// VERSIONNING
$version =& $contentObject->version( 1 );
$cli->output( "Version : " . $version );
$version->setAttribute( 'status', EZ_VERSION_STATUS_DRAFT );
$version->store();

// STORE EMPTY OBJECT
$contentObject->setAttribute('name', 'artKLJLKJ');
$contentObject->store();

// SET ATTRIBUTES
$attribs = & $contentObject->contentObjectAttributes();
foreach( $attribs as $anAttr )
{
$attID = $anAttr->attribute('contentclass_attribute_identifier');
$cli->output("Setting attribute : " . $attID );

switch( $attID )
{
case 'title' :
$anAttr->setAttribute('data_text', "Article Injection Exemple");
break;

case 'short_title' :
$anAttr->setAttribute('data_text', "NEW QUICK ARTICLE");
break;

case 'author' :
$anAttr->setAttribute('data_text', "Oscar Wilde");
break;

case 'intro' :
$anAttr->setAttribute('data_text', "Example of article injected by a client script into EZ...");
break;

case 'body' :
$anAttr->setAttribute('data_text', "The process has bean <b>automatized</b>...");
break;
}
$anAttr->store();
}
$contentObject->setAttribute('status', EZ_VERSION_STATUS_DRAFT);
$contentObject->store();


$pubProps = array(
'object_id' => $contentObject->attribute('id'),
'version' => 1
);

$operationResult = eZOperationHandler::execute( 'content', 'publish', $pubProps );
$cli->output("Publish result : " . print_r( $operationResult ));


if ( !isset( $operationResult['status'] ) || $operationResult['status'] != EZ_MODULE_OPERATION_CONTINUE )
{
if ( isset( $operationResult['result'] ) && isset( $operationResult['result']['content'] ) )
$failReason = $operationResult['result']['content'];
else
$failReason = "unknown error";

$cli->error( "Publishing failed: $failReason" );
unset( $failReason );
}

$script->shutdown();
$cli->output("Done.");
?>

Kristof Coomans

Wednesday 17 May 2006 3:25:24 am

Can you tell us what went wrong first?

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