如何从Request生成SQL?

时间:2016-11-23 17:18:44

标签: php postgresql doctrine-orm symfony

目前,我根据请求使用下一种方法来检索数据:

/**
 * @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;
}

但我需要something like this:在GET请求中指定复杂条件,例如

  • ?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

并根据此请求从存储库中获取数据。

为此目的,Symfony是否存在任何捆绑包?对任何建议都会很高兴。

1 个答案:

答案 0 :(得分:1)

使用为此用例制作的LexikFormFilterBundle,构建表单过滤器,然后从此表单过滤器构建一个学说查询。

您会找到a complete example here