How to change and entity type in Doctrine2 CTI Inheritance
PHP doesn't have support for object casting, so Doctrine doesn't support it. To workaround the problem I write this static method into parent classes:
public static function castToMe($obj) {
$class = get_called_class();
$newObj = New $class();
foreach (get_class_vars(get_class($newObj)) as $property => $value) {
if (method_exists($obj, 'get' . ucfirst($property)) && method_exists($newObj, 'set' . ucfirst($property))) {
$newObj->{'set' . ucfirst($property)}($obj->{'get' . ucfirst($property)}());
}
}
return $newObj;
}
You can create this method in class Person and use it to cast from Employe to Client and viceversa:
$employe = New Employe();
$client = Client::castToMe($employe);
Now, if you want, you can remove the $employe entity.
I was looking for this behaviour yesterday also.
In the end, after speaking with people in #doctrine on freenode, I was told that it is not possible.
If you want to do this, then you have to go through this:
Upgrading a User
- Grab the Person Entity.
- Update the discrimator column so that it is no longer a 'person' and change it to 'employee'
- Create a corresponding row inyour
Employee
table for this inheritance.
Removing Inheritance
Likewise if you want to remove inheritance, you have to..
- Grab the Person Entity.
- Update the discrimnator column so that it is no longer an 'employee' and change it to a 'person'.
- Delete the corresponding row in your
Employee
table. (Yes you have to delete it, just change the discrimator coumn is not sufficient).
This might be 7 months late, but it is at least the correct answer for anything else looking to suport such a feature.