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

图片转代码服务由CSDN问答提供 功能建议

我想用symfony为我的网站制作搜索功能。 我做了这样的表格: \ n

 &lt; div class =“input-group”id =“adv-search”&gt; 
 {{form_start(form)}} 
 
 {{form_widget(form.searchText,  {'attr':{'id':'search_box','class':'search_box form-control','placeholder':'Search'}}}}} 
 
&lt; div class =“input-group  -btn“&gt; 
&lt; div class =”btn-group“role =”group“&gt; 
 
&lt; button type =”button“id =”search_button“class =”btn btn-search“&gt  ;&lt; span 
 class =“glyphicon glyphicon-search”
 aria-hidden =“true”&gt;&lt; / span&gt;&lt; / button&gt; 
&lt; / div&gt; 
&lt; / div&gt; \  n {{form_end(form)}} 
&lt; / div&gt; 
   
 
 

接下来我得到用户写的内容:

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

我拿了用户写的内容后想要搜索 该文本的表行并返回一个数组,以便在树枝中显示它。 我正在考虑创建一个查询构建器,进行选择但它没有用。

 <  code> $ this-&gt; getEntityManager() - &gt; createQuery('SELECT u FROM ParkResortBundle:Ad u 
 WHERE你。  LIKE:string OR u.lastname LIKE:string')
  - &gt; setParameter('string','%'。$ string。'%')
  - &gt; getResult(); 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题