eZContentFunctions::createAndPublishObject not working

Author Message

Jitesh Rana

Monday 31 January 2011 2:58:24 am

Hi, I am completely new to eZ publish having following piece of code in my custom script, trying to add article, but it does not work.

<?php

require 'autoload.php';

$user = eZUser::fetchByName( 'admin' );

if (!$user){

//if no user exists let's pull out the current user:

$user = eZUser::currentUser();

}

$parent_node = eZContentObjectTreeNode::fetchByURLPath('my_folder');

$params= array();

$params['class_identifier'] = "article";

$params['creator_id'] = eZUser::currentUser()->ContentObjectID;

$params['parent_node_id'] = $parent_node->NodeID;

$params['section_id'] = $parent_node->ContentObject->SectionID;

$attributesData = array() ;

$attributesData['name'] = 'Title of Article' ;

$attributesData['short_description'] = 'This is an amazing article.' ; 
$params['attributes'] = $attributesData;

$contentObject = eZContentFunctions::createAndPublishObject($params);

eZContentCacheManager::clearObjectViewCache( $parent_node->ContentObjectID, true );

?>

It gives following error: Notice:

Warning: Invalid argument supplied for foreach() in /var/www/html/OSCAR_UAT/lib/ezutils/classes/ezmoduleoperationinfo.php on line 83 Notice: Undefined variable: definitionFile in /var/www/html/OSCAR_UAT/lib/ezutils/classes/ezmoduleoperationinfo.php on line 90

Best Regards,

Jitesh

Franck Magnan

Monday 31 January 2011 12:26:28 pm

Hello Jitesh,
I tried your code in a module of mine and it's working. So, I don't know why you got this error. Maybe your module is not set correctly.
Also, you don't need the line "require 'autoload.php';" and you can try to regenerate autoload array.

You can do this with these commands:

$ cd /var/www/html/OSCAR_UAT/
$ php bin/php/ezpgenerateautoloads.php --extension

hope this help

--
Developer at Open Wide

Jitesh Rana

Monday 31 January 2011 9:11:19 pm

Hello Franck,

I tried the code without "require 'autoload.php';" but it gives the fatal error like

Fatal error: Class 'eZUser' not found in /var/www/html/OSCAR_UAT/Test.php on line 3

So placing the line "require 'autoload.php';" again and regenerate autoload array. It gives the same error.

Warning: Invalid argument supplied for foreach() in /var/www/html/OSCAR_UAT/lib/ezutils/classes/ezmoduleoperationinfo.php on line 83PHP Notice: Undefined variable: definitionFile in /var/www/html/OSCAR_UAT/lib/ezutils/classes/ezmoduleoperationinfo.php on line 90

Is there any extra settings i have to look? Or something wrong with my eZ publish setup

How to set a module to run this custom script

Please help.

Jitesh

Franck Magnan

Tuesday 01 February 2011 12:37:23 pm

Hello Jitesh,

sorry for my poor english but my suggestion was:

  1. remove your "require autoload" instruction
  2. then, regenerate autoload array
  3. test your script

How do you launch your script? Is it by a module/view way? A cli script?
If you want to see how write a simple module and view, you can take a look to this article

--
Developer at Open Wide

Jitesh Rana

Tuesday 01 February 2011 11:50:30 pm

Hello Franck,

I am totally blank about how to launch my custom script.

Is it necessary to launch it by module/view way? If yes, can you please provide detailed steps regarding how you run this custom script in your environment?

I am having a single test.php file placed at root folder like /var/www/html/OSCAR_UAT/test.php

and browsing it like,

http://192.168.1.105/OSCAR_UAT/test.php

can you please mail your folder structure to jitesh@jzero.com for understanding ?

need your help.

Jitesh

Franck Magnan

Wednesday 02 February 2011 11:25:32 am

Hello Jitesh,
you cannot launch a php script using eZ Publish API without eZ Publish context. If you make your own module and your own view, your script will run in the eZ Publish context. You can also use php in command line. If you want to use the command line, you need to initialize eZ Publish context.
Here is a script you can launch in command line:

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

require 'autoload.php';

$cli = eZCLI::instance();

$scriptSettings = array();
$scriptSettings['description'] = 'Create a simple article in a simple folder';
$scriptSettings['use-session'] = false;
$scriptSettings['use-modules'] = true;
$scriptSettings['use-extensions'] = true;

$script = eZScript::instance( $scriptSettings );
$script->startup();

$config = '';
$argumentConfig = '';
$optionHelp = false;
$arguments = false;
$useStandardOptions = true;

$options = $script->getOptions( $config, $argumentConfig, $optionHelp, $arguments, $useStandardOptions );
$script->initialize();

createASimpleArticleForJitesh();

$script->shutdown( 0 );

function createASimpleArticleForJitesh()
{
    $user = eZUser::fetchByName( 'admin' );

    if (!$user)
    {
        //if no user exists let's pull out the current user:
        $user = eZUser::currentUser();
    }

    $parent_node = eZContentObjectTreeNode::fetchByURLPath('my_folder');
    $params= array();
    $params['class_identifier'] = "article";
    $params['creator_id'] = eZUser::currentUser()->ContentObjectID;
    $params['parent_node_id'] = $parent_node->NodeID;
    $params['section_id'] = $parent_node->ContentObject->SectionID;

    $attributesData = array() ;
    $attributesData['name'] = 'Title of Article' ;
    $attributesData['short_description'] = 'This is an amazing article.' ;
    $params['attributes'] = $attributesData;

    $contentObject = eZContentFunctions::createAndPublishObject($params);
    eZContentCacheManager::clearObjectViewCache( $parent_node->ContentObjectID, true );
}

?>

You must save it as /var/www/html/OSCAR_UAT/script.php

to execute it, open a terminal and type these commands:

$ cd /var/www/html/OSCAR_UAT
 $ php script.php

--
Developer at Open Wide

Jitesh Rana

Tuesday 08 February 2011 10:12:35 pm

Hello Franck,

sorry, for late communication.

I had launched the script in command line. it works. but, i need to make a web service that is going to call by client and create and publish objects in eZ publish system.

So, can you please guide me, how to make nusoap web service and achieve the functionality that works in eZ publish context?

one sample web service app from you will be appreciated.

need your assistance.

nehal shah

Thursday 03 March 2011 10:11:41 pm

Hi,

I want to create object in php and here is my code.

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

require 'autoload.php';

$cli = eZCLI::instance();

$scriptSettings = array();
$scriptSettings['description'] = 'Create a simple article in a simple folder';
$scriptSettings['use-session'] = false;
$scriptSettings['use-modules'] = true;
$scriptSettings['use-extensions'] = true;

$script = eZScript::instance( $scriptSettings );
$script->startup();

$config = '';
$argumentConfig = '';
$optionHelp = false;
$arguments = false;
$useStandardOptions = true;

$options = $script->getOptions( $config, $argumentConfig, $optionHelp, $arguments, $useStandardOptions );
$script->initialize();

createASimpleArticleForJitesh();

$script->shutdown( 0 );

function createASimpleArticleForJitesh()
{
$user = eZUser::fetchByName( 'jshah' );

if (!$user)
{
//if no user exists let's pull out the current user:
$user = eZUser::currentUser();
}

$parent_node = eZContentObjectTreeNode::fetchByURLPath('master_content/apa');
$params= array();
$params['class_identifier'] = "article";
$params['creator_id'] = eZUser::currentUser()->ContentObjectID;
$params['parent_node_id'] = $parent_node->NodeID;
$params['section_id'] = $parent_node->ContentObject->SectionID;

$attributesData = array() ;
$attributesData['name'] = 'Title of Article' ;
$attributesData['short_description'] = 'This is an amazing article.' ;
$params['attributes'] = $attributesData;


$contentObject = eZContentFunctions::createAndPublishObject($params);
eZContentCacheManager::clearObjectViewCache( $parent_node->ContentObjectID, true );
}

?>

Object is insrted in system successfully,but node is not created for the object, that's why there is no entry for the object in ezcontentobject_tree table.Any one have idea? Please help me.

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 18 2025 02:21:09
Script start
Timing: Jan 18 2025 02:21:09
Module start 'layout'
Timing: Jan 18 2025 02:21:09
Module start 'content'
Timing: Jan 18 2025 02:21:10
Module end 'content'
Timing: Jan 18 2025 02:21:10
Script end

Main resources:

Total runtime0.8162 sec
Peak memory usage4,096.0000 KB
Database Queries75

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0054 588.1563152.6563
Module start 'layout' 0.00540.0022 740.812539.5156
Module start 'content' 0.00760.8068 780.3281654.6094
Module end 'content' 0.81440.0017 1,434.937524.1094
Script end 0.8161  1,459.0469 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00330.4039160.0002
Check MTime0.00130.1644160.0001
Mysql Total
Database connection0.00070.084010.0007
Mysqli_queries0.725488.8846750.0097
Looping result0.00090.1071730.0000
Template Total0.780695.620.3903
Template load0.00210.255520.0010
Template processing0.778595.386020.3893
Template load and register function0.00010.017610.0001
states
state_id_array0.00120.141310.0012
state_identifier_array0.00300.363320.0015
Override
Cache load0.00200.24001250.0000
Sytem overhead
Fetch class attribute can translate value0.00070.086030.0002
Fetch class attribute name0.00160.195790.0002
XML
Image XML parsing0.00070.087830.0002
class_abstraction
Instantiating content class attribute0.00000.0034110.0000
General
dbfile0.00080.0996150.0001
String conversion0.00000.000740.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
8content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
13content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
4content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
3content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
4content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1content/datatype/view/ezxmltags/li.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/li.tplEdit templateOverride template
1content/datatype/view/ezxmltags/ol.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/ol.tplEdit templateOverride template
1content/datatype/view/ezxmltags/link.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/link.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 37
 Number of unique templates used: 10

Time used to render debug report: 0.0001 secs