Symfony 2: Inheritance and UniqueEntity workaround

Hi folks, Today I struggled over an already known bug on Symfony2 when using UniqueEntity and Entity Inheritance. In the bug discussion, @gentisaliu recommended using a custom repository, and how do this I’m documenting here. Entities: /** * @ORM\Table(name="parent") * @ORM\Entity(repositoryClass="Repository\Parent") * @UniqueEntity(fields={"name"}, repositoryMethod="findByName", message="Name already used.") * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="type", type="string") * @ORM\DiscriminatorMap({ * "a" = "ChildA", * "b" = "ChildB" * }) */ class Parent { } /** * @ORM\Entity(repositoryClass="Repository\Parent") * @ORM\Table(name="child_a") */ class ChildA extends Parent { } /** * @ORM\Entity(repositoryClass="Repository\Parent") * @ORM\Table(name="child_b") */ class ChildB extends Parent { } repositoryMethod: ...

November 27, 2013