Changing class of an existing object in eZ Publish CMS

by Ivo Lukač -

This tip is only for advanced and experienced developers, classified as "do not do this at home" and "no warranty".

A good practice in eZ Publish development is to specify the content classes (or content types) at the beginning of the project. But of course in real situations there is a big chance that something needs to be changed at later stage or even after going live. What is great with eZ is that you can easily change content types either by adding new attributes or removing the ones that are not needed. And that ability covers a lot of situation, but not all.

For example imagine a following scenario:
- a class is specified and created
- lot objects of that class are created too
- you realize that some of that objects should be of different class
- you need to convert some existing objects to that different class 

You can create the second class but the problem is that you already have the content. The swap node feature can help if the number of objects you want to change is low.  

But what to do if you need to change the class for a lot of existing objects? You could create an export/import script, but that takes time too. There was an extension for changing class on existing objects: http://projects.ez.no/object_content_class_change but its outdated (would be nice if someone could upgrade it to PHP5 and eZ 4.2+).

There is a quick solution but its not for people with weak hearth :)

  1. create the new class by copying the old one and note the new class id (e.g. 111)
  2. note all class attribute ids from the old class (e.g. 222,223)
  3. note all class attribute ids from the new class (e.g. 333,334 )
  4. collect object ids for objects you want to change class (e.g. 444,555,777,888)
  5. go to your database console and verify that you have good ids:
    select * from ezcontentobject where id in (444,555,777,888)
  6. backup your database (just in case)
  7. lock object changes to ensure that nobody changes those objects while doing this (the safest way is to just shutdown the web site or disable login for editors)
  8. run the following SQL command to update ezcontentobject table:
    update ezcontentobject set contentclass_id = 111 where id in (444,555,777,888)
  9. run following SQL commands to update ezcontentobject_attribute table for each attribute:
    update ezcontentobject_attribute set contentclassattribute_id = 333 where contentobject_id in (444,555,777,888) and contentclassattribute_id = 222;
    update ezcontentobject_attribute set contentclassattribute_id = 334 where contentobject_id in (444,555,777,888) and contentclassattribute_id = 223;
  10. Clear the related content caches
  11. Do your changes on the new class

Important thing is that you can “map” attributes only of the same datatype. Otherwise you can break the eZ data model. So the first step is crucial to create the exact same copy of the class. Afterwards, in step 11, you can do changes to your new class.So far I didn't need this to often so there is no script. It would be rather simple to create a script which could hide few steps (like 2., 3., 8., and 9.). 
DISCLAIMER: No warranty! Backup before doing this!

Comments

This site uses cookies. Some of these cookies are essential, while others help us improve your experience by providing insights into how the site is being used.

For more detailed information on the cookies we use, please check our Privacy Policy.

  • Necessary cookies enable core functionality. The website cannot function properly without these cookies, and can only be disabled by changing your browser preferences.