2013-01-16 17 views
7

Tôi đang cố gắng sử dụng giao diện dưới dạng "targetEntity". Mã đơn giản nên giải thích những gì tôi có ý định làmmục tiêu học thuyếtCấu hình giao diện

Interface:

namespace Project\Entity; 

interface AnimalInterface{ 


} 

Cát:

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 (chỉ có thể chứa một động vật;)):

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

Tôi đang sử dụng TargetEntityResolver như quy định ở đây ->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); 

Có vẻ như Resolver chỉ có thể giải quyết một Entity đến một giao diện, là người đầu tiên đưa ra một. Trong ví dụ này mèo. Doctrine xây dựng bảng AnimalFarm với một mối quan hệ (ngoại ngữ) cho con chó bảng. Khi tôi đang cố gắng thêm một con chó vào bảng thông qua EntityManager Doctrine không thành công với ErrorMessage sau: Ngoại lệ không bắt buộc 'Doctrine \ ORM \ ORMException' với thông báo 'Tìm thấy thực thể kiểu Project \ Entity \ Dog on association Project \ Entity \ AnimalFarm # animal, nhưng mong đợi Project \ Entity \ Cat 'trong [...]

Có vẻ như không thể xác định nhiều targetEntities thông qua Giao diện?

Tôi không muốn sử dụng thừa kế vì thực thể có thể triển khai nhiều giao diện. (Không thể kế thừa nhiều lần)

Bất kỳ ý tưởng nào?

Có thể từ khóa tìm kiếm tốt mà tôi có thể tìm?

Cảm ơn rất nhiều.

Trả lời

1

Điều này được lấy từ doctrine2 docs. Bạn chỉ có thể giải quyết một đối tượng bằng cách sử dụng phương pháp này.

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