doctrine2 - Filtering of associated entity collection in Symfony / Doctrine -


if have associated object collection, can restrict results?

for example: producer entity has property translations, contains collection of other entities (producertranslation).

class producer {     protected $id;     // arraycollection     protected $translations; } 

producercontroller:

$producers = $this->getdoctrine()     ->getrepository('producerbundle:producer')     ->findall(); 

result:

producer     id: 1     translations:         en: producertranslation         de: producertranslation 

it's alright. want 1 entity of language. expected result:

$producers = $this->getdoctrine()     ->getrepository('producerbundle:producer')     ->findbylocale('en');  producer     id: 1     translations:         en: producertranslation 

how it?

to restrict sub collection can use querybuilder (assuming locale property of producertranslation):

$qb = $this->getentitymanager()->createquerybuilder();  $qb->select('p, pt')     ->from('producerbundle:producer', 'p')     ->join('p.translations', 'pt')     ->where($qb->expr()->eq('pt.locale', ':locale'))     ->setparameter('locale', 'en')     ->getquery()     ->getresult(); 

that'll want. note select('p, pt') part important fetch items want collection result.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -