I have a Parent entity with 2 child entities (Foo and Bar) implementing SINGLE_TABLE inheritance.

Is it possible to create a new Parent() entity and dynamically set it's discriminator to foo instead of creating a new Foo() ?

有帮助吗?

解决方案

No, there isn't, if you really need the scenario mentioned in your comment, then you'll probably be better with some kind of factory method:

abstract class MyParent
{
    public static function fromString($type)
    {
        switch ($type) {
            case 'foo':
                return new Foo();
            case 'bar':
                return new Bar();
        }
        throw new DomainException('Unknown type: ' . $type);
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top