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 [email protected] 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.