多个条件搜索查询构建器[symfony2]

时间:2016-09-06 10:49:15

标签: php mysql symfony doctrine-orm query-builder

我创建了一个搜索表单,我正在尝试从数据库中获取以下数据(airportairport1departuredateprice),我可以只获得[airport]而不是其他实体。

这是我的控制器

public function searchtabflightResultAction(Request $request)
{


    $form = $this->createForm(new SearchflightType());

    if ($this->get('request')->getMethod() == 'POST')
    {
        $form->handleRequest($request);

    $em = $this->getDoctrine()->getManager();
    $entities = $em->getRepository('FLYBookingsBundle:Post')->searchflight($form['airport']->getData());

    } else {
        throw $this->createNotFoundException('The page you were looking for doesn\'t exist.');
    }

    return $this->render('FLYBookingsBundle:Post:searchtabflightResult.html.twig', array('entities' => $entities));
}

PostRepository.php

public function searchflight($entity)
    {
        $qb = $this->createQueryBuilder('u')
            ->select('u')
            ->where('u.airport like :entity')
            ->andWhere('u.airport1 like :entity')
            ->orderBy('u.id')
            ->setParameter('entity', '%'.$entity.'%');
        return $qb->getQuery()->getResult();
    }

1 个答案:

答案 0 :(得分:1)

尝试类似:

在你的控制器中:

$entities = $em->getRepository('FLYBookingsBundle:Post')->searchflight($form);

在您的回购中:

public function searchflight(FormInterface $form)
{
    $qb = $this->createQueryBuilder('u');

    if ($form->has('airport')) {
        $qb->andWhere(
            $qb->expr()->like('u.airport', ':airport')
        )
           ->setParameter('airport', '%'. $form->get('airport')->getData().'%')
    }

    // ... complete like this with the other params