doctrine targetEntity interfaces

7
我正在尝试使用接口作为“targetEntity”。 简单的代码应该能够解释我想做什么。
接口:
namespace Project\Entity;

interface AnimalInterface{


}

翻译:猫:
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;
}

翻译:狗:
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;
}

动物农场(可以只包含一种动物 ;)):
 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;
     }
}

我正在按照这里指定的方法使用TargetEntityResolver -> 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);

似乎Resolver只能将一个实体解析为一个接口,即第一个给定的接口。在这个例子中,cat,Doctrine使用关系(外键)将表AnimalFarm与表dog建立起来。当我尝试通过EntityManager将一只狗添加到表中时,Doctrine会出现以下错误消息: Uncaught exception 'Doctrine\ORM\ORMException' with message 'Found entity of type Project\Entity\Dog on association Project\Entity\AnimalFarm#animal, but expecting Project\Entity\Cat' in [...]
看起来不可能通过一个接口定义多个targetEntities?
我不想使用继承,因为实体可以实现多个接口。(不支持多重继承)
有什么想法吗?
也许有好的搜索关键词我可以查找吗?
非常感谢。
1个回答

1
这段内容来自doctrine2文档。使用此方法只能解析一个对象。
/**
 * 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();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接