Interfaces of the doctrine of targetEntity

I am trying to use interfaces as "targetEntity". Simple code should explain what I intend to do.

Interface:

namespace Project\Entity; interface AnimalInterface{ } 

Cat:

 namespace Project\Entity; use Doctrine\ORM\Mapping as ORM; use Project\Entity\AnimalInterface; /** * Represents an Invoice. * * @ORM\Entity * @ORM\Table(name="Cat") */ class Cat implements AnimalInterface { /** * @var int * @ORM\Id @ORM\Column(type="integer", name="id") * @ORM\GeneratedValue */ protected $id; } 

Dog:

 namespace Project\Entity; use Doctrine\ORM\Mapping as ORM; use Project\Entity\AnimalInterface; /** * @ORM\Entity * @ORM\Table(name="Dog") */ class Dog implements AnimalInterface { /** * @var int * @ORM\Id @ORM\Column(type="integer", name="id") * @ORM\GeneratedValue */ protected $id; } 

AnimalFarm (can contain only one animal;)):

  namespace Project\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="AnimalFarm") */ class AnimalFarm { /** * * @var int * @ORM\Id @ORM\Column(type="integer", name="id") * @ORM\GeneratedValue */ protected $id; /** * @ORM\ManyToOne(targetEntity="Project\Entity\AnimalInterface") * @var AnimalInterface */ protected $animal; public function setAnimal(AnimalInterface $animal){ $this->animal = $animal; } } 

I am using TargetEntityResolver as stated here -> http://symfony.com/doc/master/cookbook/doctrine/resolve_target_entity.html

bootstrap.php (Zend):

  $em = $doctrine->getEntityManager(); $evm = $em->getEventManager(); $listener = new \Doctrine\ORM\Tools\ResolveTargetEntityListener(); $listener->addResolveTargetEntity( 'Project\Entity\AnimalInterface', 'Project\Entity\Dog', array() ); $listener->addResolveTargetEntity( 'Project\Entity\AnimalInterface', 'Project\Entity\Cat', array() ); $evm->addEventListener(Doctrine\ORM\Events::loadClassMetadata, $listener); 

It seems that Resolver can only allow one Entity per interface, the first given. In this example, cat. Doctrine builds an AnimalFarm table with a relation (foreignkey) to the table dog. When I try to add a dog to the table through the Doctrine EntityManager, I cannot run the following ErrorMessage command: Uncaught exception 'Doctrine \ ORM \ ORMException' with the message 'Found object of type Project \ Entity \ Dog in the association Project \ Entity \ AnimalFarm # animal, but waiting Project \ Entity \ Cat 'in [...]

It seems impossible to define multiple targets through an interface?

I do not want to use inheritance because Entities can implement multiple interfaces. (Multiple inheritance is not possible)

Any ideas?

Perhaps the good search keywords I can find?

Many thanks.

+7
source share
1 answer

This is taken from doctrine2 docs . You can only allow one object using this method.

 /** * An interface that the invoice Subject object should implement. * In most circumstances, only a single object should implement * this interface as the ResolveTargetEntityListener can only * change the target to a single object. */ interface InvoiceSubjectInterface { // List any additional methods that your InvoiceModule // will need to access on the subject so that you can // be sure that you have access to those methods. /** * @return string */ public function getName(); } 
+1
source

All Articles