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

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 02:04:14
Script start
Timing: Jan 18 2025 02:04:14
Module start 'layout'
Timing: Jan 18 2025 02:04:14
Module start 'content'
Timing: Jan 18 2025 02:04:15
Module end 'content'
Timing: Jan 18 2025 02:04:15
Script end

Main resources:

Total runtime1.4032 sec
Peak memory usage4,096.0000 KB
Database Queries60

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0069 588.1563152.6563
Module start 'layout' 0.00690.0030 740.812539.5156
Module start 'content' 0.00991.3916 780.3281556.7813
Module end 'content' 1.40150.0016 1,337.109416.1094
Script end 1.4031  1,353.2188 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00310.2232160.0002
Check MTime0.00140.0969160.0001
Mysql Total
Database connection0.00110.079810.0011
Mysqli_queries1.346695.9661600.0224
Looping result0.00060.0407580.0000
Template Total1.367997.520.6840
Template load0.00220.156420.0011
Template processing1.365797.328120.6829
Template load and register function0.00010.007110.0001
states
state_id_array0.00180.126410.0018
state_identifier_array0.00210.151220.0011
Override
Cache load0.00180.1290270.0001
Sytem overhead
Fetch class attribute can translate value0.00060.045220.0003
Fetch class attribute name0.00140.102660.0002
XML
Image XML parsing0.00100.069220.0005
class_abstraction
Instantiating content class attribute0.00000.001380.0000
General
dbfile0.00090.0663220.0000
String conversion0.00000.000640.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.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
4content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
4content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
4content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
1content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 15
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs