Nick
Nick

Reputation: 10143

How to generate SQL from Request?

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

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

Answers (1)

Veve
Veve

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

Related Questions