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!

eZ debug

Timing: Jan 17 2025 21:39:12
Script start
Timing: Jan 17 2025 21:39:12
Module start 'content'
Timing: Jan 17 2025 21:39:12
Module end 'content'
Timing: Jan 17 2025 21:39:12
Script end

Main resources:

Total runtime0.2606 sec
Peak memory usage4,096.0000 KB
Database Queries223

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0079 589.1641180.8281
Module start 'content' 0.00790.1571 769.9922883.0625
Module end 'content' 0.16500.0955 1,653.0547343.8047
Script end 0.2605  1,996.8594 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00371.4188210.0002
Check MTime0.00140.5297210.0001
Mysql Total
Database connection0.00130.496610.0013
Mysqli_queries0.166663.95422230.0007
Looping result0.00170.67102210.0000
Template Total0.233489.620.1167
Template load0.00200.761420.0010
Template processing0.231488.819120.1157
Template load and register function0.00020.067910.0002
states
state_id_array0.00140.530020.0007
state_identifier_array0.00090.336630.0003
Override
Cache load0.00180.6999700.0000
Sytem overhead
Fetch class attribute name0.00120.4428110.0001
Fetch class attribute can translate value0.00080.303260.0001
class_abstraction
Instantiating content class attribute0.00000.0070120.0000
XML
Image XML parsing0.00220.843960.0004
General
dbfile0.00361.3870480.0001
String conversion0.00000.002330.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.tplforum_topic/full.tplextension/community_design/design/suncana/override/templates/forum_topic/full.tplEdit templateOverride template
7content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
15content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
5content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
6content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
2content/datatype/view/ezxmltags/emphasize.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/emphasize.tplEdit templateOverride template
3content/datatype/view/ezxmltags/link.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/link.tplEdit templateOverride template
1content/datatype/view/ezxmltags/strong.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/strong.tplEdit templateOverride template
1content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1content/datatype/view/ezxmltags/quote.tpldatatype/ezxmltext/quote.tplextension/ezwebin/design/ezwebin/override/templates/datatype/ezxmltext/quote.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 43
 Number of unique templates used: 11

Time used to render debug report: 0.0002 secs