0

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); 
);`
bohr
  • 1,206
  • 1
  • 9
  • 11
  • show us your deparment entity definition and the relation "entite" – Bartłomiej Wach May 20 '14 at 09:24
  • Here it is: `/** * @ORM\ManyToOne(targetEntity="XXX\perimetreBundle\Entity\EntiteResponsable") * @ORM\JoinColumn(nullable=false) */ private $entite;` – bohr May 20 '14 at 09:35
  • And the departement one: `/** * Departement * * @ORM\Table(name="departements") * @ORM\Entity(repositoryClass="XXX\perimetreBundle\Entity\DepartementRepository") */ class Departement {` – bohr May 20 '14 at 09:41
  • `public function getAvailableDepartements( $entites ) { $depts = new \Doctrine\Common\Collections\Collection(); $dept = Array(); foreach ($entites->toArray() as $ent){ $dept = $this->findBy(Array('entite' => $ent)); foreach($dept as $d){ if(!$depts->contains($d)) $depts->add ($d); } } return $depts; }` I have written this, which works but doesnt get me expected result. – bohr May 20 '14 at 09:55
  • Have you tried using expressions? Try this answer: http://stackoverflow.com/a/6319643 – Bartek May 20 '14 at 11:09
  • Thanks for your answers. I tried this: `$arr = $entites->toArray(); $qb = $this->createQueryBuilder('d'); $qb->add('where', $qb->expr()->in('d.entite', '?param')); $qb->setParameter('param', $arr); return $qb;` But still doesnt work :( If i remove the where clause and setparameter it works. – bohr May 20 '14 at 11:52
  • Hello ? Please people, im asking for good practices, i could do manual ugly query to fix this ! It's the same people that cry about bad code and don't give answers about good one... – bohr May 20 '14 at 12:13
  • if you answer with code, best is to edit your question and paste it there, so it is readable – Bartłomiej Wach May 20 '14 at 17:12

1 Answers1

1

Are you sure you want to return $qb ?

Try returning something like this:

$query = $qb->getQuery();
$result = $query->getResult(); // or $result = $query->getArrayResult();

return $result;

That's what my repository methods return.

tomazahlin
  • 2,137
  • 1
  • 24
  • 29
  • Yeah i'm sure, because I use it in the POST_SUMBIT event of a XXXType form, and this is the following section of the callback closure that use the repo's method. `$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); }));` This is annoying yes, but Im trying to respect sep of concerns. – bohr May 20 '14 at 14:35