Doctrine ORM:未定义的偏移量2

问题描述 投票:2回答:1

我正在处理从数据库中获取一些结果的请求,但我有一个错误,我不知道如何重新获得它

在我的存储库中:

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()
        ;
}

在我的控制器中:

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(),
    ]);
}

我有一个错误“通知:未定义的偏移量:2”

但是,当我在phpMyAdmin中发出此请求时,它会返回我要查找的内容

php doctrine-query
1个回答
0
投票

谢谢你的反馈,我找到了解决方案,在我的方法findBySearchField中我给了数组而不是给出变量。此解决方案有效:

    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(),
        ]);
    }
© www.soinside.com 2019 - 2024. All rights reserved.