In my symfony 2 app, I have a repository for my "departement" entity. In this repo, i have a method called getAvailableDepartements, that i use in a XXXType + Ajax to get all the departements that fit previous choices in the form:
public function getAvailableDepartements( $entites ) {
$qb = $this->createQueryBuilder('d');
$qb->where('d.entite IN :ents')
->setParameter('ents', $entites);
return $qb;
}
My problem is about best practice, the code above doesn't work and I would like to know how other symfony devs would do what I try to achieve: retrieving all the departements that their "entite" field is IN the $entites Collection in parameter... I was thinking:
- foreach entity in entities, if ok populate local collections and return the merged collection that contains all departement ?
or use kind of these:
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) { parent::findBy($criteria, $orderBy, $limit, $offset);
}
but it seems both ugly.
Thanks for your help and advices !
EDIT: Call of this method in the XXType class:
`$form->add('departements', 'entity', array(
'class' => 'XXXperimetreBundle:Departement',
'property' => 'sigle', 'multiple' => true,
'expanded' => true,
'query_builder' => function(DepartementRepository $dr) use ($entites) { return $er->getAvailableDepartements($entites);
);`