Forums / Developer / Right way to create an eZ object using PHP

Right way to create an eZ object using PHP

Author Message

Hakim Bouras

Friday 12 March 2010 3:41:37 am

Hi,

I have a few questions about the right way to create an ez object, using PHP.

I was not able to find a place where there are clear guidelines on the subject. So from different places I checked (forum, eZpedia, API code) I figured out the following steps :

1/ getting an instance of the class of the object

2/ creating an object from the class (adding records in tables ezcontentobject and ezcontentobject_attribute )
$object = $class->instantiate( $userID, $sectionID, $version, $languageCode, eZContentObjectVersion::STATUS_INTERNAL_DRAFT );

3/ setting internal object attributes using
$object->setAttribute( <attribute_name>, <attribute_value> );
(...);
$object->store();

4/ setting other attributes using }
$dataMap = $object->attribute( 'data_map' );
$attribute = $dataMap[ <attribute_name> ];
$attribute->setAttribute( <attribute_type>, <attribute_value> );
$attribute->sync();

5/ creating a node assignement (adding record a record in table eznode_assignment)
$nodeAssignment = $object->createNodeAssignment( $parentNodeID,
$isMain, $remoteID,
$class->attribute( 'sort_field' ),
$class->attribute( 'sort_order' ) );

What is the purpose of this step - is it mandatory ? What the assignment is used for in ez Publish ?It is not clear to understand this assignment of a node that does not yet exist (there are no ezcontentobject_tree object created at this step)

6/ publishing the object, which is doing a lot of things and along them:

  • create a node (main node)
  • update the publishing date of the object
  • update the op_code of the nodeAsssignment
  • call custom edit handler and workflow scripts
  • ...

$operationResult = eZOperationHandler::execute( 'content',
'publish',
array( 'object_id' => $object->ID,
'version' => $object->CurrentVersion )
);

We need to fetch again the object, for the $object to hold of the changes made during the publish process. Is there a way to directly pass the $object to a publish process so that we do not need to refetch the current object ?

Is this process correct ? Are there any "more efficient way" to do it ?

And a last question about object updating.

Is there a way to update a published object without having to republish it ? When I only "store" my changes, they are not directely available, unless I republish the object - which is a problem when updating an object in a workflow or a custom edit handler (because it makes a loop)

Thanks for all your feedback, and once I get the answers I will post the global method I wrote to handle object creation and hide the complexity.

Hakim Bouras
PRISMALOG - Creation de site Internet

Matthieu Sévère

Friday 12 March 2010 6:42:56 am

Hello Hakim,

I'm using this :

$params = array();                
$params['parent_node_id'] = $ini->variable('purchase_settings','stockists_folder_node_id');   
$params['class_identifier'] = 'stockist'; 
$params['creator_id'] = 14; // admin
$params['attributes'] = $attributesData;
$contentObject = eZContentFunctions::createAndPublishObject( $params );

In attributesDate you store an associative array with all the attributes of your object

You can also look here : http://pubsvn.ez.no/websvn2/listing.php?repname=nextgen&path=/trunk/tests/toolkit/extras/kernelwrapper/#path_trunk_tests_toolkit_extras_kernelwrapper_

You'll find a kernel wrapper. I think eZ Systems use it for their unit tests.

Cheers

--
eZ certified developer: http://ez.no/certification/verify/346216

Hakim Bouras

Monday 15 March 2010 3:31:12 am

Hi Matthieu,

Thanks for your answer.

I did not know about the eZContentFunctions::createAndPublishObject method and about the kernel wrapper classes (ezpClass, ezpObject and ezpNode) available in the "Test" folder.

Both are interresting sources to understand the process, and gives some more information to handle attributes, versions and language versions.

I am still wondering :

  • what the NodeAssignment is used for ?
  • is there a way to update object attributes without republishing the object?

Hakim

Matthieu Sévère

Monday 15 March 2010 4:07:49 am

A node is used to build your content tree.

A object is an instance of your content type.

A node assignment is used to link your object to a node and then give him a position in your content tree.

I don't really understand what you mean by republish the object but basically when you edit an object is create a draft you can store it for later editing or publish it so that it becomes your published version.

--
eZ certified developer: http://ez.no/certification/verify/346216

Hakim Bouras

Monday 15 March 2010 5:50:39 am

>>A node assignment is used to link your object to a node and then give him a position in your content tree.

From what I have seen in the structure of a NodeAssignment, there is no information about the content tree (which is built in the TreeNode objects - ezcontentobject_tree table).

It looks like there is a relation 1 to 1 or 1 to 0 between the two tables and there is an "op_code" field and a "from_node_id" (which is set either to 0 or -1) in the table eznode_assignment that are not available in ezcontentobject_tree.

There might be a reason why of this structure but I do not get it.

>> I don't really understand what you mean by republish the object
>> but basically when you edit an object is create a draft you can store it
>> for later editing or publish it so that it becomes your published version.

I was talking about updating an object through a php script, not through the user interface. I do not think that draft versions are created automatically when you update the attribute of an object, I suspect that you would have to create explicitely a draft version of the object.

Hakim

Igor Vrdoljak

Thursday 18 March 2010 10:44:35 pm

Hi Hakim

You can change your content objects without republishing them. I use it rather often, due to heavy processing included in publishing new version of an object, which makes it impractical if you are doing a batch of work including changing a few thousand objects.

The procedure is very simple, you fetch current version of your object, change the attributes and store the changes. The last thing you need to do is to clear the caches, so that your view cache would be correctly created.

$contentObject = $node->attribute('object');

// get the current version,you can also set the modified timestamp
$version = $contentObject->version($contentObject->attribute('current_version'));
$version->setAttribute( 'modified', eZDateTime::currentTimeStamp() );

// change the attributes
...
...

// store the changes and clear view cache of the object
$contentObject->store();
$contentObject->expireAllViewCache();

// also, you may have need to change the object name:
$class = $contentObject->contentClass();
$contentObject->setName( $class->contentObjectName( $contentObject ) );
$contentObject->store();

http://www.netgen.hr/eng
http://twitter.com/ivrdoljak

Hakim Bouras

Friday 26 March 2010 4:14:01 am

Thanks Igor for your explanations. It works fine, and the part about updating the name was very usefull as well.

Cheers,

Hakim

Alex Kozeka

Thursday 04 November 2010 5:35:06 am

Hi,

Could someone also help to find correct way of setting default sort order for newly created or already existing node?

I thought that the code:

$nodeAssignment = $object->createNodeAssignment( $parentNodeID,$isMain, $remoteID,$class->attribute( 'sort_field' ),$class->attribute( 'sort_order' ) );

should do the job, however it didn't.

Internal research showed that 'ezcontentobject_tree' table stores child nodes sort type and sort order, and not 'eznode_assignment' (but it also has related fields) but didn't find place in code where it is used.

--
Argentea S.p.A., http://www.argentea.it/

eZ debug

Timing: Jan 18 2025 00:01:43
Script start
Timing: Jan 18 2025 00:01:43
Module start 'content'
Timing: Jan 18 2025 00:01:44
Module end 'content'
Timing: Jan 18 2025 00:01:44
Script end

Main resources:

Total runtime1.1158 sec
Peak memory usage4,096.0000 KB
Database Queries214

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0065 587.8438180.8125
Module start 'content' 0.00650.9859 768.6563748.9844
Module end 'content' 0.99240.1233 1,517.6406348.4297
Script end 1.1156  1,866.0703 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00440.3977210.0002
Check MTime0.00150.1323210.0001
Mysql Total
Database connection0.00080.068010.0008
Mysqli_queries0.986488.40162140.0046
Looping result0.00240.21452120.0000
Template Total1.036592.920.5182
Template load0.00190.174120.0010
Template processing1.034592.719820.5173
Template load and register function0.00020.015810.0002
states
state_id_array0.04794.291910.0479
state_identifier_array0.00170.148720.0008
Override
Cache load0.00220.19401330.0000
Sytem overhead
Fetch class attribute can translate value0.00160.142350.0003
Fetch class attribute name0.00160.1421120.0001
XML
Image XML parsing0.00600.541550.0012
class_abstraction
Instantiating content class attribute0.00000.0034160.0000
General
dbfile0.01441.2909450.0003
String conversion0.00000.000830.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/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
8content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
24content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
11content/datatype/view/ezxmltags/strong.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/strong.tplEdit templateOverride template
18content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
17content/datatype/view/ezxmltags/emphasize.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/emphasize.tplEdit templateOverride template
2content/datatype/view/ezxmltags/li.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/li.tplEdit templateOverride template
2content/datatype/view/ezxmltags/ul.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/ul.tplEdit templateOverride template
1content/datatype/view/ezxmltags/link.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/link.tplEdit templateOverride template
2content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 95
 Number of unique templates used: 12

Time used to render debug report: 0.0002 secs