【问题标题】:doctrine targetEntity interfaces学说 targetEntity 接口
【发布时间】:2013-01-16 13:39:26
【问题描述】:

我正在尝试将接口用作“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;
}

AnimalFarm(只能包含一只动物;)):

 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);

似乎解析器只能将一个实体解析为一个接口,第一个给定的。在这个例子中猫。 Doctrine 建立表 AnimalFarm 与表狗的关系(外键)。 当我尝试通过 EntityManager Doctrine 将狗添加到表中时,会出现以下错误消息: 未捕获的异常“Doctrine\ORM\ORMException”,消息“在关联 Project\Entity\AnimalFarm#animal 上找到类型为 Project\Entity\Dog 的实体,但在 [...] 中期待 Project\Entity\Cat”

好像不能通过一个接口定义多个targetEntities?

我不想使用继承,因为实体可以实现多个接口。 (不能多重继承)

有什么想法吗?

也许我可以找到好的搜索关键字?

非常感谢。

【问题讨论】:

    标签: zend-framework doctrine


    【解决方案1】:

    这取自doctrine2 docs。使用此方法只能解析一个对象。

    /**
     * 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多