Forums / Developer / Versioning when adding content class attribute from PHP

Versioning when adding content class attribute from PHP

Author Message

Eirik Alfstad Johansen

Friday 15 April 2011 4:19:16 am

Hi,

I have created the project http://projects.ez.no/nmcontentclass which is supposed to make it easier to work with a class specification, and have that spec imported as new and modified classes into eZ Publish.

The extension works as intended, except for one thing: When I try to add an attribute to an existing class, the attribute is only displayed when creating a new object from that class, and not when modifying an object which existed before the attribute was added.

I suspect that this is related to class and/or class attribute versioning, but I can't wrap my head around how this is supposed to work.

When a attribute is created, here's an exerpt of code that is run (I've removed some code which I assume is irrelevant). For more code, check out the updateClasses() method in http://svn.projects.ez.no/nmcontentclass/trunk/classes/contentclass.php

// create attribute$attribute = eZContentClassAttribute::create( $classID, $dataType, array(), $languageLocale);
$dt = $attribute->dataType();
$dt->initializeClassAttribute( $attribute );
$attribute->store();

// update attribute
$attrParams = array();
$attrParams['identifier'] = $attributeData['identifier'];
$attrParams['name'] = $attributeData['name'];
$attrParams['is_searchable'] = 1;
$attrParams['is_required'] = 0;
$attrParams['placement'] = $attributeData['placement'];
$attrParams['version'] = $classVersionID; // this is defined earlier in the code

foreach($attrParams as $key => $val)
{
if($val != $attribute->attribute($key))
{
$attribute->setAttribute( $key, $val);
}
}

$attribute->store();

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Edi Modrić

Friday 15 April 2011 7:59:37 am

Hi Eirik,

When adding class attribute through PHP, you also need to manually add the instance of that attribute to all of the existing objects manually.

There is a nice PHP script, addmissingobjectattributes.php, that does exactly that, so you can check it out.

It is located at:

https://github.com/ezsystems/ezscriptmonitor/blob/master/bin/addmissingobjectattributes.php

eZ Publish certified developer

http://ez.no/certification/verify/350658

Eirik Alfstad Johansen

Monday 25 April 2011 11:45:08 pm

Hi Edi,

Thanks a bunch. I was not aware of this.

However, I'm curious as to whether this happens automatically when an attribute is added through the admin interface, as this seems to be the case.

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Edi Modrić

Tuesday 26 April 2011 12:32:35 am

Hi Eirik,

yes, admin interface does it automatically when you add new attributes to content classes.

The reason for mentioned script existance is that if there are many objects of the specific class, the operation of saving the class can be very long process which can timeout and leave the database inconsistent. The script works around this by using a stored draft of the class and running through the objects adding/removing attributes as needed.

eZ Publish certified developer

http://ez.no/certification/verify/350658

eZ debug

Timing: Jan 29 2025 13:42:57
Script start
Timing: Jan 29 2025 13:42:57
Module start 'content'
Timing: Jan 29 2025 13:42:57
Module end 'content'
Timing: Jan 29 2025 13:42:58
Script end

Main resources:

Total runtime0.1315 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0064 588.1719180.8203
Module start 'content' 0.00640.0047 768.992297.9922
Module end 'content' 0.01110.1203 866.9844527.8594
Script end 0.1314  1,394.8438 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00312.3636200.0002
Check MTime0.00120.9167200.0001
Mysql Total
Database connection0.00090.671510.0009
Mysqli_queries0.083663.59991410.0006
Looping result0.00080.63221390.0000
Template Total0.120091.310.1200
Template load0.00060.492910.0006
Template processing0.119490.789010.1194
Override
Cache load0.00040.326210.0004
Sytem overhead
Fetch class attribute can translate value0.00100.759110.0010
XML
Image XML parsing0.00030.217410.0003
General
dbfile0.00997.5637200.0005
String conversion0.00000.003830.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
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0002 secs