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