2

I'm working on a request to pick up some result from the database but I have an error and I don't know how to resove it

in my repository :

public function findBySearchField($salary, $contract, $experience, $training)
{
    return $this->createQueryBuilder('a')
        ->andWhere('a.salary = :sal')
            ->setParameter('sal', $salary)
        ->andWhere('a.contract = :con')
            ->setParameter('con', $contract)
        ->andWhere('a.experience = :exp')
            ->setParameter('exp', $experience)
        ->andWhere('a.training = :tra')
            ->setParameter('tra', $training)
        ->orderBy('a.postedAt', 'DESC')
        ->getQuery()
        ->getResult()
        ;
}

in my controller :

public function index(Request $request, AdvertRepository $advertRepository): Response
{
    $form = $this->createForm(ResearchType::class);
    $form->handleRequest($request);

    if ($request->getMethod() == 'POST') {
        $salary = $form->get('salary')->getData();
        $contract = $form->get('contract')->getData();
        $experience = $form->get('experience')->getData();
        $training = $form->get('training')->getData();

        return $this->render('advert/index.html.twig', [
            'form' => $form->createView(),
            'adverts' => $advertRepository->findBySearchField(
                ['salary' => $salary],
                ['contract' => $contract],
                ['experience' => $experience],
                ['training' => $training]
            ),
        ]);
    }

    return $this->render('advert/index.html.twig', [
        'form' => $form->createView(),
        'adverts' => $advertRepository->findAll(),
    ]);
}

I have an error "Notice: Undefined offset: 2"

However when I make this request in phpMyAdmin it returns what I am looking for

BanjoSf4
  • 149
  • 1
  • 1
  • 13

1 Answers1

0

thank you for your feedback, I found the solution, in my method findBySearchField I gave arrays instead of giving variables. This solution works :

    public function index(Request $request, AdvertRepository $advertRepository): Response
    {
        $form = $this->createForm(ResearchType::class);
        $form->handleRequest($request);

        if ($request->getMethod() == 'POST') {
            return $this->render('advert/index.html.twig', [
                'form' => $form->createView(),
                'adverts' => $advertRepository->findBySearchField(
                    $salary     = $form->get('salary')->getData(),
                    $contract   = $form->get('contract')->getData(),
                    $experience = $form->get('experience')->getData(),
                    $training   = $form->get('training')->getData()
                ),
            ]);
        }

        return $this->render('advert/index.html.twig', [
            'form' => $form->createView(),
            'adverts' => $advertRepository->findAll(),
        ]);
    }
BanjoSf4
  • 149
  • 1
  • 1
  • 13