Forums / Discussions / eZContentFunctions::createAndPublishObject not working

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.

eZ debug

Timing: Jan 17 2025 23:42:17
Script start
Timing: Jan 17 2025 23:42:17
Module start 'content'
Timing: Jan 17 2025 23:42:18
Module end 'content'
Timing: Jan 17 2025 23:42:18
Script end

Main resources:

Total runtime0.8829 sec
Peak memory usage4,096.0000 KB
Database Queries212

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0071 589.3828180.8203
Module start 'content' 0.00710.6890 770.2031659.7188
Module end 'content' 0.69610.1867 1,429.9219345.0391
Script end 0.8828  1,774.9609 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00440.4971210.0002
Check MTime0.00170.1878210.0001
Mysql Total
Database connection0.00070.075710.0007
Mysqli_queries0.762286.32842120.0036
Looping result0.00260.29352100.0000
Template Total0.847896.020.4239
Template load0.00220.245220.0011
Template processing0.845795.786420.4228
Template load and register function0.00040.041210.0004
states
state_id_array0.00120.137310.0012
state_identifier_array0.00080.085620.0004
Override
Cache load0.00220.24601250.0000
Sytem overhead
Fetch class attribute can translate value0.00150.165740.0004
Fetch class attribute name0.00200.231490.0002
XML
Image XML parsing0.00110.119140.0003
class_abstraction
Instantiating content class attribute0.00000.0036110.0000
General
dbfile0.01061.2024260.0004
String conversion0.00000.001330.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
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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 37
 Number of unique templates used: 10

Time used to render debug report: 0.0002 secs