Creating and updating content in code
This page is for application creators who write content from code: imports, synchronizations, forms that create
objects, migrations. The kernel's eZContentFunctions (kernel/classes/ezcontentfunctions.php) creates and publishes in
one call, with the same operation (and triggers) as the editor.
Contents
- Creating and publishing
- Updating
- Values for each datatype
- Idempotent imports with remote IDs
- Moving, hiding, removing
- Large operations
Creating and publishing
$object = eZContentFunctions::createAndPublishObject( array(
'creator_id' => $userID,
'class_identifier' => 'article',
'parent_node_id' => $parentNodeID,
'remote_id' => 'feed-item-' . $externalID,
'attributes' => array(
'title' => 'A new article',
'intro' => $introXml,
'body' => $bodyXml ) ) );
if ( !$object instanceof eZContentObject )
throw new RuntimeException( 'Could not create the article' );Attribute values are given in their fromString() form. Publishing runs the content/publish operation, so workflows
on its triggers run too, and caches and the search index are updated.
Updating
eZContentFunctions::updateAndPublishObject( $object, array(
'attributes' => array( 'title' => 'A better title' ) ) );A new version is created and published; the previous one becomes archived, like an editor's edit.
Values for each datatype
| Datatype | String form |
|---|---|
ezstring, eztext | the text |
ezinteger, ezfloat, ezboolean | 42, 3.5, 1 |
ezdate, ezdatetime | a Unix timestamp |
ezxmltext | the internal XML of rich text (the <section> document) |
ezobjectrelationlist | object IDs separated by - |
ezselection | the selected option IDs |
ezimage, ezbinaryfile | a file path (and for images an alternative text after a separator) |
The exact format of each is in its datatype class's fromString() in kernel/classes/datatypes/<name>/; read it before
importing a datatype for the first time.
Idempotent imports with remote IDs
Give imported objects a remote ID built from the source's ID. On the next run, update instead of create:
$remoteID = 'feed-item-' . $externalID;
$existing = eZContentObject::fetchByRemoteID( $remoteID );
if ( $existing )
eZContentFunctions::updateAndPublishObject( $existing, array( 'attributes' => $attributes ) );
else
eZContentFunctions::createAndPublishObject( array( 'remote_id' => $remoteID ) + $params );Running an import twice must not create duplicates.
Moving, hiding, removing
Use the kernel operations rather than SQL: eZContentObjectTreeNodeOperations::move(), eZContentOperationCollection
for hide and reveal, eZContentObjectTreeNode::removeSubtrees() for removal (to the trash or not). They keep URL aliases,
caches and the search index right.
Large operations
Thousands of objects belong in a background process: a command run from cron, a queue job
(Queues with expqueue), or the kernel's content jobs for removes, copies and moves of large
subtrees (Content jobs). Commit in batches, clear eZContentObject::clearCache() between batches,
and log progress.
Related pages
- The content API in PHP
- CLI scripts with eZScript
- Workflows, triggers and events
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.