Forums / Developer / Modify and Publish Object with PHP

Modify and Publish Object with PHP

Author Message

Erik Weinmaster

Friday 25 June 2010 10:29:53 am

I've found the function createAndPublishObject which works great if you are creating objects. But what about editing them? I've looked on this site and found some references to using something along the lines of:

$contentObjectAttribute->fromString($newValue);

$contentObjectAttribute->store();

However, this doesn't follow the eZ Publish 'Publishing' process. What id does is changes the attribute of the object. What it doesn't do is increment the version, and thus keep track of the history. I am trying to write another function to do this, and below is what I have so far.

static function modifyAndPublishObject( $contentObject, $contentAttributes = null )
{
if(is_object($contentObject))
{
$db = eZDB::instance();
$db->begin();
$currentVersion = $contentObject->currentVersion();
$nextVersion = eZContentObjectVersion::create($contentObject->ID, false, $currentVersion->attribute('version') + 1);
$nextVersion->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
$nextVersion->store();
if (is_array($contentAttributes) && count($contentAttributes) > 0)
{
$attributes = $contentObject->attribute( 'contentobject_attributes' );
foreach( $attributes as $attribute )
{
$attributeIdentifier = $attribute->attribute( 'contentclass_attribute_identifier' );
if ( isset( $contentAttributes[$attributeIdentifier] ) )
{
$dataString = $contentAttributes[$attributeIdentifier];
switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) )
{
case 'ezimage':
case 'ezbinaryfile':
case 'ezmedia':
{
$dataString = $storageDir . $dataString;
break;
}
default:
}
$attribute->fromString( $dataString );
$attribute->store();
}
}
}
$db->commit();
$operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObject->attribute( 'id' ),
'version' => $nextVersion->attribute('version') ) );
}
else
{
eZDebug::writeError( "Parameter contentObject not actual object.", 'CCContentFunctions::modifyAndPublishObject' );
}
return $contentObject;
}

This almost works. Unfortunately, what happens is that the new version of the object gets created, but BOTH the new version and previous version get the changes. How can I write this so that ONLY the new version has the changes?

Thanks,

-erik

Vincent Tabary

Friday 25 June 2010 11:43:09 am

Hi Erik !

You should look at this function :

eZContentFunctions::updateAndPublishObject()

I think it is what you are looking for.

About your function, you have updated the attributes from the $contentObject. You have to update the attributes directly from the $nextVersion

Look at this source on line 308

Vinz
http://vincent.tabary.me

Erik Weinmaster

Monday 28 June 2010 7:23:19 am

Thanks! I updated the kernel file eZContentFunctions.php from 4.2 to 4.3 and used the updateAndPublishObject. Works great.

eZ debug

Timing: Jan 18 2025 10:20:54
Script start
Timing: Jan 18 2025 10:20:54
Module start 'content'
Timing: Jan 18 2025 10:20:55
Module end 'content'
Timing: Jan 18 2025 10:20:55
Script end

Main resources:

Total runtime0.8848 sec
Peak memory usage4,096.0000 KB
Database Queries195

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0077 587.7344180.8281
Module start 'content' 0.00770.7483 768.5625516.4531
Module end 'content' 0.75600.1288 1,285.0156333.4688
Script end 0.8847  1,618.4844 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00440.4941210.0002
Check MTime0.00150.1701210.0001
Mysql Total
Database connection0.00090.097510.0009
Mysqli_queries0.803090.75811950.0041
Looping result0.00250.28681930.0000
Template Total0.847495.820.4237
Template load0.00180.204020.0009
Template processing0.845695.568520.4228
Template load and register function0.00030.032410.0003
states
state_id_array0.00150.170810.0015
state_identifier_array0.00120.139220.0006
Override
Cache load0.00160.1835660.0000
Sytem overhead
Fetch class attribute can translate value0.00290.329430.0010
Fetch class attribute name0.00140.162330.0005
XML
Image XML parsing0.00050.057730.0002
class_abstraction
Instantiating content class attribute0.00000.001030.0000
General
dbfile0.00510.5787210.0002
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
3content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
6content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
1content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
2content/datatype/view/ezxmltags/link.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/link.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 14
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs