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!

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

Main resources:

Total runtime0.2492 sec
Peak memory usage4,096.0000 KB
Database Queries86

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0058 592.5078152.6250
Module start 'layout' 0.00580.0040 745.132839.4453
Module start 'content' 0.00980.2378 784.5781881.9844
Module end 'content' 0.24750.0016 1,666.562526.3984
Script end 0.2491  1,692.9609 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00321.2975160.0002
Check MTime0.00140.5604160.0001
Mysql Total
Database connection0.00070.295810.0007
Mysqli_queries0.149059.7982860.0017
Looping result0.00100.3950840.0000
Template Total0.209684.120.1048
Template load0.00250.989720.0012
Template processing0.207183.113420.1035
Template load and register function0.00020.080710.0002
states
state_id_array0.00170.677820.0008
state_identifier_array0.00130.518130.0004
Override
Cache load0.00220.8946700.0000
Sytem overhead
Fetch class attribute name0.00160.6390110.0001
Fetch class attribute can translate value0.00110.429450.0002
class_abstraction
Instantiating content class attribute0.00000.0100120.0000
XML
Image XML parsing0.00441.782150.0009
General
dbfile0.00401.6240370.0001
String conversion0.00000.004040.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.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
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 43
 Number of unique templates used: 11

Time used to render debug report: 0.0001 secs