2013-01-16 17 views
7

Sto cercando di utilizzare le interfacce come "targetEntity". Il semplice codice dovrebbe spiegare quello che intendo faredoctrine targetEntity interfaces

Interfaccia:

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

Cane:

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 (può solo contenere un animale;)):

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

sto usando il TargetEntityResolver come specificato qui ->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); 

Sembra che il Resolver in grado di risolvere una sola entità a un'interfaccia, il primo dato uno. In questo esempio cat. Dottrina costruisci la tabella AnimalFarm con una relazione (straniera) con il cane da tavola. Quando sto cercando di aggiungere un cane alla tabella tramite la Dottrina di EntityManager non riesce con il seguente messaggio Error: Eccezione non rilevata 'Doctrine \ ORM \ ORMException' con messaggio 'Entità trovata di tipo Progetto \ Entità \ Cane sull'associazione Progetto \ Entità \ AnimalFarm # animal, ma in attesa di Project \ Entity \ Cat 'in [...]

Sembra che non sia possibile definire più TargetEntities tramite un'interfaccia?

Non voglio utilizzare l'ereditarietà perché le entità possono implementare più interfacce. (Eredità multipla non possibile)

Qualche idea?

Forse le parole chiave di ricerca buone possono essere cercate?

Grazie mille.

risposta

1

Questo è preso da doctrine2 docs. Puoi risolvere un solo oggetto usando questo metodo.

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