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.
js
$('#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);
          }
    });
});
The controller
    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->getRequest()->request->get('searchText');
        //$string = "alfa";
        $users = $this->getDoctrine()
                     ->getRepository('ApanaMainBundle:User')
                     ->findByLetters($string);
        //return users on json format
        $encoders = array(new XmlEncoder(), new JsonEncoder());
        $normalizers = array(new GetSetMethodNormalizer());
        $serializer = new Serializer($normalizers, $encoders);
        $jsonContent = $serializer->serialize($users, 'json');
        $response = new Response($jsonContent);
        return $response;
    }
}
User Repository
    class UserRepository extends EntityRepository
{
    public function findByLetters($string){
        return $this->getEntityManager()->createQuery('SELECT u FROM ApanaMainBundle:User u  
                WHERE u.firstname LIKE :string OR u.lastname LIKE :string')
                ->setParameter('string','%'.$string.'%')
                ->getResult();
    }
}
If I give a static text for my string parameter and visit the route for the controller, it works fine.
 
     
    