php - 多条件搜索查询生成器 [symfony2]

标签 php mysql symfony doctrine-orm query-builder

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

这是我的 Controller :

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

最佳答案

尝试类似的东西:

在你的 Controller 中:

$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

关于php - 多条件搜索查询生成器 [symfony2],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39347178/

相关文章:

mysql - Eloquent 查询结果受关系标准限制

php - Symfony 2 session 在通过 AJAX 调用创建后不会持续存在

javascript - 将图像上传到运行时创建的指定文件夹和子文件夹

php - 由于文件名中有空格,取消链接失败

php - 我只从 android json 中的 mysql 数据库获取最后一个 id

mysql - 我的数据库表上的并发更新有问题

symfony - Lexik JWT 返回 401 未经授权

symfony - 为什么 Symfony 3.3.13 源代码会生成弃用警告(不在我的代码中)?

php - 检查下一行是否重复

nginx 服务器有时会返回 404 Not Found 以获得有效的 URL