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 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?