0

I can't implement search functionality with Symfony. This is what I have:

/**
 * CoffeeShop controller.
 *
 * @Route("/coffeeshops")
 */
class CoffeeShopController extends Controller
{

/**
 * @Route("/q?", name="api_coffeeshops_search")
 * @Method("GET")
 */
public function searchAction(Request $request){

    $chain = $request->query->get('chain');

    $result = new JsonResponse();
    $coffeeShops = $this->getDoctrine()
        ->getRepository('CoreBundle:CoffeeShop')->findBy(array('chain' => $chain));

    $result->setContent($coffeeShops);
    return $result;
}
}

And when I submit query

 http://127.0.0.1:8000/api/v1/coffeeshops/q?chain=2

I only get null as a response

blahblah
  • 1,010
  • 15
  • 40
  • 5
    You make sure that the "chain" 2 exists in database? However the char `?` in you route path definition there is unnecessary. – yceruto Aug 22 '16 at 14:53
  • First step would be to be sure that there is a `chain : 2` entry in your database. Is it an id ? Or a number ? – Florent Destremau Aug 23 '16 at 09:08
  • Yes, there is chain with id 2 – blahblah Aug 23 '16 at 11:07
  • Hi, if you confirm that $coffeeShops has elements.. and $chain set correctly.. check jsonResponse, symfony version?? http://stackoverflow.com/a/11715894/2209876 – Maxi Schvindt Aug 23 '16 at 12:45
  • 1
    Some thoughts: 1. remove the `?` in your route definition. 2. You try to select the entry by id? Then your findBy is wrong: `$coffeeShops = $this->getDoctrine() ->getRepository('CoreBundle:CoffeeShop')->findBy(array('id' => $chain));` 3. Just a reminder `query->get` should be sanitized for security reasons or user `query->filter` 4. Is chain an related entity to your CoffeeShop entity? Then you should first select the chain by id and pass that result to your CoffeeShop query. – Joe Aug 23 '16 at 13:03

0 Answers0