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

eZ debug

Timing: Jan 19 2025 20:10:24
Script start
Timing: Jan 19 2025 20:10:24
Module start 'content'
Timing: Jan 19 2025 20:10:25
Module end 'content'
Timing: Jan 19 2025 20:10:25
Script end

Main resources:

Total runtime1.4792 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.0064 588.9531180.8359
Module start 'content' 0.00641.3244 769.7891714.3672
Module end 'content' 1.33080.1483 1,484.1563353.0625
Script end 1.4791  1,837.2188 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00400.2723210.0002
Check MTime0.00160.1080210.0001
Mysql Total
Database connection0.00060.043110.0006
Mysqli_queries1.322889.42802080.0064
Looping result0.00280.18802060.0000
Template Total1.446897.820.7234
Template load0.00190.126420.0009
Template processing1.444997.680020.7224
Template load and register function0.00010.006910.0001
states
state_id_array0.00170.117910.0017
state_identifier_array0.00120.082720.0006
Override
Cache load0.00230.15742980.0000
Sytem overhead
Fetch class attribute can translate value0.00220.145840.0005
Fetch class attribute name0.00110.0761100.0001
XML
Image XML parsing0.00330.221140.0008
class_abstraction
Instantiating content class attribute0.00000.0026140.0000
General
dbfile0.00700.4731380.0002
String conversion0.00000.000330.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/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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 64
 Number of unique templates used: 6

Time used to render debug report: 0.0002 secs