How to change and entity type in Doctrine2 CTI Inheritance

纵饮孤独 提交于 2019-11-29 06:34:51

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

  1. Grab the Person Entity.
  2. Update the discrimator column so that it is no longer a 'person' and change it to 'employee'
  3. Create a corresponding row inyour Employee table for this inheritance.

Removing Inheritance

Likewise if you want to remove inheritance, you have to..

  1. Grab the Person Entity.
  2. Update the discrimnator column so that it is no longer an 'employee' and change it to a 'person'.
  3. 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.

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.

pi.

In Doctrine2, when you have your parent entity class, Person set as:

/**
 * @Entity
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"person" = "Person", "employee" = "Employee", , "client" = "Client"})
 */
class Person
{
    // ...
}

and sub classes such as Client set as:

/** @Entity */
class Client extends Person
{
    // ...
}

when you instantiate Person as:

$person = new Person();

Doctrine2 checks your @DiscriminatorMap statement (above) for a corresponding mapping to Person and when found, creates a string value in the table column set in @DiscriminatorColumn above.

So when you decide to have an instance of Client as:

$client = new Client();

Following these principles, Doctrine2 will create an instance for you as long as you have declared the parameters in the @DiscriminatorMap. Also an entry will be made on the Person table, in the discr column to reflect that type of entity class that has just been instantiated.

Hope that helps. It's all in the documentation though

You could do something like this though:

This Trait can be used on your Repository class:

namespace App\Doctrine\Repository;

trait DiscriminatorTrait
{
    abstract public function getClassMetadata();

    abstract public function getEntityManager();

    private function updateDiscriminatorColumn($id, $class)
    {
        $classMetadata = $this->getClassMetadata();

        if (!in_array($class, $classMetadata->discriminatorMap)) {
            throw new \Exception("invalid discriminator class: " . $class);
        }

        $identifier = $classMetadata->fieldMappings[$classMetadata->identifier[0]]["columnName"];

        $column = $classMetadata->discriminatorColumn["fieldName"];
        $value = array_search($class, $classMetadata->discriminatorMap);

        $connection = $this->getEntityManager()->getConnection();

        $connection->update(
            $classMetadata->table["name"],
            [$column => $value],
            [$identifier => $id]
        );
    }
}

There still might be some extra work you need to put in, like clearing values in fields that are only present on one of your sub-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!