doushao8421 2016-01-26 07:07
浏览 40
已采纳

如何在symfony中创建查询构建器

I want to make a search functionality for my website with symfony. I did this form :

 <div class="input-group" id="adv-search">
                {{ form_start(form) }}

                {{ form_widget(form.searchText,{'attr':{'id':'search_box','class':'search_box form-control ','placeholder':'Search'}}) }}

                <div class="input-group-btn">
                    <div class="btn-group" role="group">

                        <button type="button" id="search_button" class="btn btn-search"><span
                                    class="glyphicon glyphicon-search"
                                    aria-hidden="true"></span></button>
                    </div>
                </div>
                {{ form_end(form) }}
            </div>

Next I get what user write:

if ($form->isSubmitted()) {
            $data = $form->getData();
            $string = $data['searchText'];
             echo '<pre>';
            \Doctrine\Common\Util\Debug::dump($string);
            exit;
            echo '</pre>';
        }

After I take what user wrote and I want to search in the table rows for that text and return an array in order to display it in twig. I was thinking about making a query builder,making a select but it didn't worked .

$this->getEntityManager()->createQuery('SELECT u FROM ParkResortBundle:Ad u
                WHERE u. LIKE :string OR u.lastname LIKE :string')
            ->setParameter('string','%'.$string.'%')
            ->getResult();
        }
  • 写回答

2条回答 默认 最新

  • dqb14659 2016-01-26 07:42
    关注

    You may need to use Orx Expression in your search controller

    Try adding use statement in search controller.

    use Doctrine\ORM\Query\Expr\Orx;

    then your query builder should be like createQueryBuilder not with createQuery.

    $qb = $this->getDoctrine()->getEntityManager()->getRepository( 'ParkResortBundle:Ad' )->createQueryBuilder( 'p' );
    $qb->add( 'where',
        $qb->expr()->orX(
            $qb->expr()->like( 'p.firstname', "'%{$SearchKeyword}%'" ),
            $qb->expr()->like( 'p.lastname', "'%{$SearchKeyword}%'" )
        )
    );
    $searchResult = $qb->getQuery()->getResult();
    

    Edit 1:

    To use OrderBy with query builder:

    $qb = $this->getDoctrine()->getEntityManager()->getRepository( 'ParkResortBundle:Ad' )->createQueryBuilder( 'p' );
        $qb->add( 'where',
            $qb->expr()->orX(
                $qb->expr()->like( 'p.firstname', "'%{$SearchKeyword}%'" ),
                $qb->expr()->like( 'p.lastname', "'%{$SearchKeyword}%'" )
            )
        );
        $qb->add('orderBy', 'p.firstname ASC');
        $searchResult = $qb->getQuery()->getResult();
    

    Here is a complete list of helper methods available in QueryBuilder:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端