title: Custom search using doctrine and symfony2
body: <p>I want to create a searchController, that searches the db for whatever keywords the user enters into a textbox. I have looked at <a href="https://stackoverflow.com/questions/9033474/search-for-several-arguments-with-doctrine">this article</a> and <a href="https://stackoverflow.com/questions/11528435/database-searching-using-doctrine-and-symfony2">this</a> and many more.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>public function searchAction(Request $request) {
          if ($request-&gt;getMEthod() == 'GET') {
              $title = $request-&gt;get('Search_term');
              //echo "&lt;div class=\"searchText\"&gt;Search Results&lt;/div&gt;&lt;hr/&gt;";
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $Search_terms = explode(' ', $title); //splits search terms at spaces
              $query = "SELECT * FROM Entity/Adverts WHERE ";
  
              foreach ($Search_terms as $each) {
                  // echo $each."&lt;br/&gt;";
                  $i = 0;
                  $i++;
                  if ($i == 1)
                      $query .= "Adv_title LIKE '%$each%' ";
                  else
                      $query .= "OR Adv_title LIKE '%$each%' ";
              }
              $query = $em-&gt;createQuery($query);
              $numRow = $query-&gt;count();
              if ($numRow &gt; 0) {
                  while ($query-&gt;fetch()) {
                      $repository = $em-&gt;getRepository('YCRYcrBundle:Adverts')-&gt;findBy(array(
                          'advTitle' =&gt; $title
                      ));
  
                      /*    echo "&lt;h2&gt;&lt;a href='#'&gt; $title&lt;/a&gt; &lt;/h2&gt;";
                        echo "$desc &lt;br /&gt; &lt;br /&gt;";
                        echo"&lt;a href='/201308/View/YCR/index.php' class='link-button right'&gt;&lt;span&gt;Apply&lt;/span&gt;&lt;/a&gt;"; */
                  }
              }
              /* else
                echo "none found for \"&lt;b&gt;$SearchTerm &lt;/b&gt;\"&lt;/br&gt;Check spelling"; */
          }
          return $this-&gt;render('YCRYcrBundle:Search:search.html.twig', array('title' =&gt; $title-&gt;getAdvTitle()));
  }
  </code></pre>
  
  <p>What is missing from this code to make it work, and or what is wrong?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>sorry of I was unclear. I am getting the following error:</p>
  
  <blockquote>
    <p>FatalErrorException: Error: Call to undefined method Doctrine\ORM\Query::count() in C:\wamp\www\201308\src\YCR\YcrBundle\Controller\SearchController.php line 28
    and do not know what is wrong as I am new to Symfony and doctrine. </p>
  </blockquote>
  
  <p>This is what I have in my search.html.twig:</p>
  
  <pre><code>{% extends "YCRYcrBundle::layout.html.twig" %}
  
  {% block content %}
  
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
      &lt;head&gt;
           &lt;meta charset="utf-8" /&gt;
          &lt;title&gt;YCR Job Search&lt;/title&gt;
  
          &lt;link href="stylesheets/style.css" rel="stylesheet" type="text/css" /&gt;
    &lt;link href="stylesheets/colour.css" rel="stylesheet" type="text/css" /&gt;
          &lt;/head&gt;
          &lt;body onload="b = setInterval('clear()', 0);"&gt;
  
  
              &lt;div class="topDiv"&gt;
                  &lt;div style="float: left;"&gt;
                      &lt;img src="/201308/View/images/ycr.jpg" alt="ycr"&gt;&lt;/div&gt;
                  &lt;br/&gt;
  
                  &lt;!--&lt;H2&gt;Search for a Job&lt;/H2&gt;--&gt; 
                  &lt;div class="searchform"&gt;
      &lt;form id="formsearch" name="p" method="get" action="index.php"&gt;
            &lt;span&gt;
  
            &lt;input name="Search_term" class="editbox_search" id="editbox_search"          maxlength="80" value="&lt;?php $this; ?&gt;" type="text" /&gt;
            &lt;/span&gt;
                &lt;input type="image" name="button_search" src="images/search.gif"     class="button_search" alt="" /&gt;
          &lt;/form&gt;
                  &lt;br/&gt;          
                  &lt;div id="search_results"&gt;
  
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;!-- &lt;/div&gt;--&gt;
  
              &lt;script type="text/javascript" src="http://code.jquery.com/jquery-    1.7.2.min.js"&gt;&lt;/script&gt;&lt;!--javascript jquery library--&gt;
              &lt;script type="text/javascript" src="../View/scripts/Script.js"&gt;&lt;/script&gt;         
  
  
  {% endblock %}
  </code></pre>
  
  <p>When the user searches, for example programmer, to display like this:</p>
  
  <pre><code>-Programmer
  
  -a summary of the description of what a programmer entails
  
  -a button/link that reads: read more, that takes me to a page with full description
  </code></pre>