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 :)
- create the new class by copying the old one and note the new class id (e.g. 111)
- note all class attribute ids from the old class (e.g. 222,223)
- note all class attribute ids from the new class (e.g. 333,334 )
- collect object ids for objects you want to change class (e.g. 444,555,777,888)
- go to your database console and verify that you have good ids:
select * from ezcontentobject where id in (444,555,777,888) - backup your database (just in case)
- 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)
- run the following SQL command to update ezcontentobject table:
update ezcontentobject set contentclass_id = 111 where id in (444,555,777,888) - 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; - Clear the related content caches
- 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!