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; class Cat implements AnimalInterface { protected $id; }
Dog:
namespace Project\Entity; use Doctrine\ORM\Mapping as ORM; use Project\Entity\AnimalInterface; class Dog implements AnimalInterface { protected $id; }
AnimalFarm (can contain only one animal;)):
namespace Project\Entity; use Doctrine\ORM\Mapping as ORM; class AnimalFarm { protected $id; 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.
roccosportal.com
source share