2013-08-20 16 views
5

Ho la seguente struttura di entità nel mio progetto.Indice non definito: joinColumns doctrine + symfony2

class MyEntity 
{ 
[... some more fields...] 
/** 
* @Type("array<string, string>") 
* @ORM\ManyToMany(targetEntity="Me\MyBundle\Entity\Example") 
* @ORM\JoinTable(name="example_myentity", 
*  joinColumns={@ORM\JoinColumn(name="plan_id", referencedColumnName="id")}, 
*  inverseJoinColumns={@ORM\JoinColumn(name="example", referencedColumnName="label")} 
*) 
*/ 
private $example; 
} 



class Example 
{ 

/** 
* @ORM\Id 
* @ORM\Column(name="label", type="string", length=50, unique=true, nullable=false) 
*/ 
private $label; 
} 

Quando ho cercato di ottenere "$ esempio" utilizzando la funzione findBy() da Dottrina ho ricevuto il seguente avviso:

Undefined index: joinColumns

ho cercato di eseguire il debug esso, e il problema sembra essere nel file dottrina BasicEntityPersister.php nella funzione

_getSelectEntitiesSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null), 

Ho osservato nello stack trace che il secondo parametro "$ assoc" è sempre nullo e penso che sia per questo che Doctrine non esegue l'istruzione JOIN.

Qualche idea?

Grazie

+0

Hai aggiornato il database? – rpg600

+0

Sì, è tutto aggiornato. Database aggiornato, cache cancellata ... – user2528085

+0

Se sono molti a molti, non dovrebbe essere MyEntity. $ Esempio essere $ esempi? Lo stai inizializzando come array di doctrine nel costruttore? http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html – Cerad

risposta

4

credo che questo sia un bug, possibilmente this one.

Ho lo stesso problema e l'unica soluzione per ora sembra essere attendere un aggiornamento per BasicEntityPersister o scrivere la query che è abbastanza semplice.

Prima aggiungere un repository per la vostra classe

/** 
* MyClass 
* 
* @ORM\Table() 
* @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\MyClassRepository") 
*/ 
class MyClass 
... 

Poi, nel tuo repositoryclass:

class MyClassRepository extends EntityRepository 
{ 
    public function findByExample(Example $example) 
    { 
     return $this->getEntityManager() 
       ->createQueryBuilder() 
       ->select('m') 
       ->from('AcmeDemoBundle:MyClass', 'm') 
       ->innerJoin('m.examples', 'e') 
       ->where('e.id = :exampleid') 
       ->setParameter('exampleid', $example->getId()) 
       ->getQuery() 
       ->getResult(); 
    } 
} 

Poi finalmente è possibile ottenere tutte le istanze MyClass correlato a un esempio da:

$this->getDoctrine()->getManager()->getRepository('AcmeDemoBundle:MyClass')->findByExample($example); 
+0

Anche io credo che sia un bug, ho creato il seguente bug report: http://www.doctrine-project.org/jira/browse/DDC-2988 Che contiene anche una patch che risolve il problema. –

+0

Il collegamento al bug è morto – shakaran

+0

@shakaran - grazie, ho aggiornato il collegamento –

Problemi correlati