title: Symfony2 - Ajax Search
body: <p>I am working on a Symfony2 project where I have a user entity and I need an ajax search bar to search for my users.
  The problem is that in my AJAX response the controller, for some reason, returns all users from the database.</p>
  
  <p>js</p>
  
  <pre><code>$('#search').keyup(function() {
  
       searchText = $(this).val();
  
       $.ajax({
          type: "GET",
          url: "/Apana/web/app_dev.php/search",
          dataType: "json",
          data: {searchText : searchText},
          success : function(response) 
            {
                  console.log(response);
            }
      });
  });
  </code></pre>
  
  <p>The controller</p>
  
  <pre><code>    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\Response;
  
  use Symfony\Component\Serializer\Serializer;
  use Symfony\Component\Serializer\Encoder\XmlEncoder;
  use Symfony\Component\Serializer\Encoder\JsonEncoder;
  use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
  
  use Apana\Bundle\MainBundle\Entity\User;
  
  class SearchController extends Controller
  {
  
      public function liveSearchAction(Request $request)
      {
  
          $string = $this-&gt;getRequest()-&gt;request-&gt;get('searchText');
          //$string = "alfa";
          $users = $this-&gt;getDoctrine()
                       -&gt;getRepository('ApanaMainBundle:User')
                       -&gt;findByLetters($string);
  
          //return users on json format
  
          $encoders = array(new XmlEncoder(), new JsonEncoder());
          $normalizers = array(new GetSetMethodNormalizer());
          $serializer = new Serializer($normalizers, $encoders);
  
          $jsonContent = $serializer-&gt;serialize($users, 'json');
  
          $response = new Response($jsonContent);
          return $response;
      }
  }
  </code></pre>
  
  <p>User Repository</p>
  
  <pre><code>    class UserRepository extends EntityRepository
  {
  
      public function findByLetters($string){
          return $this-&gt;getEntityManager()-&gt;createQuery('SELECT u FROM ApanaMainBundle:User u  
                  WHERE u.firstname LIKE :string OR u.lastname LIKE :string')
                  -&gt;setParameter('string','%'.$string.'%')
                  -&gt;getResult();
      }
  }
  </code></pre>
  
  <p>If I give a static text for my string parameter and visit the route for the controller, it works fine.</p>