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

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 19 2025 03:42:52
Script start
Timing: Jan 19 2025 03:42:52
Module start 'layout'
Timing: Jan 19 2025 03:42:52
Module start 'content'
Timing: Jan 19 2025 03:42:53
Module end 'content'
Timing: Jan 19 2025 03:42:53
Script end

Main resources:

Total runtime0.7690 sec
Peak memory usage4,096.0000 KB
Database Queries71

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0053 589.1563152.6250
Module start 'layout' 0.00530.0032 741.781339.4453
Module start 'content' 0.00850.7589 781.2266709.2891
Module end 'content' 0.76740.0015 1,490.515628.1641
Script end 0.7690  1,518.6797 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00390.5016160.0002
Check MTime0.00140.1836160.0001
Mysql Total
Database connection0.00060.074710.0006
Mysqli_queries0.634082.4369710.0089
Looping result0.00080.0991690.0000
Template Total0.732095.220.3660
Template load0.00320.414320.0016
Template processing0.728894.767220.3644
Template load and register function0.00020.021110.0002
states
state_id_array0.00130.174010.0013
state_identifier_array0.00140.180320.0007
Override
Cache load0.00350.45502980.0000
Sytem overhead
Fetch class attribute can translate value0.00050.070530.0002
Fetch class attribute name0.00130.1667100.0001
XML
Image XML parsing0.00320.413530.0011
class_abstraction
Instantiating content class attribute0.00000.0048140.0000
General
dbfile0.00260.3413270.0001
String conversion0.00000.001440.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
7content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
7content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
21content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
27content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 64
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs