Reputation: 10143
Currently I use next approach for retreiving data according to request:
/**
* @QueryParam(name="filters", nullable=true, map=true, description="Filter by fields. Must be an array ie. &filters[id]=3")
*/
public function cgetAction(ParamFetcherInterface $paramFetcher)
{
$filters = $paramFetcher->get('filters') ?: [];
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository($this->entityClassName())
->findBy($filters);
return $entities;
}
But I need something like this: specify complex conditions in GET request, for example
?filter={"where":{"or":[{"id":1},{"id":2},...,{"id":20"},{"id":21}]}}
?filter[where][date][gt]=2014-04-01T18:30:00.000Z
?filter={"where": {"keywords": {"inq": ["foo", "bar"]}}}
?filter[where][and][0][title]=My%20Post&filter[where][and][1][content]=Hello
and get data from repository in according to this request.
Does exist any bundle for Symfony for this purpose? Will be glad for any advice.
Upvotes: 1
Views: 110
Reputation: 6748
Use the LexikFormFilterBundle, it's made for this use case, building form filters and then build a doctrine query from this form filter.
You'll find a complete example here.
Upvotes: 1