Search code examples
phpsymfony4doctrine-query

How to set multiple parameters for the same key in Doctrine Query


I'm setting up an advanced search for a jobboard and I need to find resumes by contract, knowing that a resume can have multiple contracts.

I have a form where you can choose which type of contract you are looking for (It's a ChoiceType::class with multiple => true)

In my repository :

public function findByContract(array $contract)
{
    return $this->createQueryBuilder('r')
        ->andWhere('r.contract = :con')
        ->setParameter('con', array($contract))
        ->getQuery()
        ->getResult()
    ;
}

In my controller :

public function index(Request $request, ResumeRepository $resumeRepository)
{
    $formSearch = $this->createForm(ResumeSearchFormType::class);
    $formSearch->handleRequest($request);

    if ($formSearch->isSubmitted() && $formSearch->isValid()) {
        $data = $formSearch->getData();

        $r = $resumeRepository->findByContract($data->getContract());
        var_dump($r); die;

This var_dump() returns an empty array.

I don't know how to set multiple parameters for the same key


Solution

  • Use IN condition:

    public function findByContract(array $contract)
    {
        return $this->createQueryBuilder('r')
            ->andWhere('r.contract IN (:contracts)')
            ->setParameter('contracts', $contract)
            ->getQuery()
            ->getResult()
        ;
    }