2013-01-16 11 views
7

Próbuję użyć interfejsów jako "targetEntity". Prosty kod powinien wyjaśnić, co mam zamiar zrobićDoctrine targetIntity interfejsów

Interfejs:

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

Pies:

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 (może po prostu zawierać jedno zwierzę;)):

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

Używam TargetEntityResolver jak określono tutaj ->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); 

Wydaje resolver może rozwiązać tylko jeden podmiot do interfejsu, pierwszy dany. W tym przykładzie cat. Doktryna buduje stół AnimalFarm ze związkiem (obcym) do psa stołowego. Gdy próbuję dodać psa do tabeli za pomocą Doktryny EntityManager, kończy się niepowodzeniem z następującym komunikatem ErrorMessage: Brak przechwyconego wyjątku "Doctrine \ ORM \ ORMException" z komunikatem "Znaleziono obiekt typu Project \ Entity \ Dog w stowarzyszeniu Project \ Entity \ AnimalFarm # animal, ale spodziewa się Project \ Entity \ Cat 'w [...]

Czy nie można zdefiniować wielu targetEntities za pomocą interfejsu?

Nie chcę używać dziedziczenia, ponieważ jednostki mogą implementować wiele interfejsów. (Wielokrotne dziedziczenie nie jest możliwe)

Jakieś pomysły?

Może dobrze wyszukiwane słowa kluczowe mogę szukać?

Dziękuję bardzo.

Odpowiedz

1

Jest to wzięte z doctrine2 docs. Możesz rozwiązać tylko jeden obiekt, używając tej metody.

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