Forums / Discussions / Creating eZ Publish Objects in PHP

Friday 04 June 2010 8:09:02 am - 6 replies

» Read full tutorial

Introduction

This tutorial will show you how to create eZ Publish objects using PHP to allow you to dynamically create or import content from elsewhere. After reading this article you should be able to create objects for any situation including those with XML fields, image or file fields and object relations.

Author Message

Jérôme Vieilledent

Wednesday 09 June 2010 5:09:26 am

Hi David

Thanks for this piece of knowledge :)

However, I noticed that you access information from your objects directly from their properties :

//setting general node details
$params = array();
$params ['class_identifier'] = 'folder'; //class name (found within setup=>classes in the admin if you need it
$params['creator_id'] = $user->ContentObjectID;//using the user created above
$params['parent_node_id']=$parent_node->NodeID;//pulling the node id out of the parent
$params['section_id'] = $parent_node->ContentObject->SectionID;

This is a bad practice as you should access these informations using the attribute() accessor method, as eZContentObject inherits from eZPersistentObject. With this accessor, you can access to the very same properties than in templates as described in the online doc :

//setting general node details
$params = array();
$params ['class_identifier'] = 'folder'; //class name (found within setup=>classes in the admin if you need it
$params['creator_id'] = $user->attribute( 'contentobject_id' ); //using the user created above
$params['parent_node_id']=$parent_node->attribute( 'node_id' );//pulling the node id out of the parent
$params['section_id'] = $parent_node->attribute( 'object' )->attribute( 'section_id' );

This notice is available for all PHP classes inheriting from eZPersistentObject, which is the case for number of eZ Publish PHP classes. To view all available properties for these classes, see their static method definition() (field and function_attributes keys). See eZContentObject::definition() in API doc.

Another quick note about XML blocks (rich text) : It's better to use the input parser from eZOE extension (eZOEInputParser), which supports lots of additional HTML tags rather than eZSimplifiedInputParser ;-)

Walter Rafelsberger

Sunday 13 June 2010 7:35:06 am

Hi,

There are a lot of examples how to access and manipulate ez publish
objects directly from PHP and not ez publish templates yours.
But all those examples require the php script to run using ez
publish's CLI/ezscript setup.

What I'm trying to achieve is to be able to use an ez publish instance
from within another web application, in this case an app written using
ez components' MVCTools.

Is there a way to initialize ez publish (like here
http://share.ez.no/tutorials/ez-publish/creating-ez-publish-objects-in-php/%28page%29/2)
but not for a CLI-environment?

Thanks and kind regards,
Walter

Jérôme Renard

Monday 13 September 2010 6:25:13 am

An alternative to the standard CLI script which takes time to write is to use ezexec.php.

For example create /tmp/foo.php with the following content:

<?php
print_r(eZContentObjectTreeNode::fetch(2));
?>

And then run

php ./bin/php/ezexec.php /tmp/foo.php

And you will see that your script will be executed within an eZ Publish context.

That's useful for debugging or running quick tests.

:)

Nicolas Pastorino

Monday 13 September 2010 6:35:37 am

"

An alternative to the standard CLI script which takes time to write is to use ezexec.php.

For example create /tmp/foo.php with the following content:

<?php
print_r(eZContentObjectTreeNode::fetch(2));
?>

And then run

php ./bin/php/ezexec.php /tmp/foo.php

And you will see that your script will be executed within an eZ Publish context.

That's useful for debugging or running quick tests.

:)

"

Extremely useful, indeed !

Thanks for the hint Jérôme ;)

--
Nicolas Pastorino
Director Community - eZ
Member of the Community Project Board

eZ Publish Community on twitter: http://twitter.com/ezcommunity

t : http://twitter.com/jeanvoye
G+ : http://plus.tl/jeanvoye

Eirik Alfstad Johansen

Monday 08 November 2010 3:03:00 am

An extremely thorough and useful article, even for an eZ Publish veteran like myself. ;)

I would add, though, that allthough placing your scripts directly in the /bin/php/ folder of your eZP installation is fine for development purposes, you should place it in its own extension before deploying in order to ensure an easier upgrade process.

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Nicolas Pastorino

Monday 08 November 2010 3:22:59 am

Thanks for this veteran remark ;)

Cheers,

--
Nicolas Pastorino
Director Community - eZ
Member of the Community Project Board

eZ Publish Community on twitter: http://twitter.com/ezcommunity

t : http://twitter.com/jeanvoye
G+ : http://plus.tl/jeanvoye

You must be logged in to post messages in this topic!