教义一次设定一个标准,而不是一次设定所有标准


Doctrine Set Multiple Criteria One At A Time Instead Of All At Once

我正在使用KendoUI框架GRID,它返回用户当前过滤器设置的JSON。

对于过滤器,其结果如下所示。

"filter":{"logic":"and","filters":[{"field":"name","operator":"contains","value":"o"},{"field":"name","operator":"startswith","value":"w"}]}

我需要遍历这个数组,并为要过滤的教义创建标准。 我看到的每个示例都显示所有内容同时发生,这不适用于我的场景。

我也在使用Symfony 3.0,所以下面的代码创建了存储库。

$repository = $this->getDoctrine()->getRepository('AppBundle:Company');

然后我目前对其余代码执行此操作。

$company_total = $repository->findAll();
        $company_records = $repository->findBy(array(),$sort,$pageSize,($page-1)*$pageSize);
        $data["total"] = count($company_total);
        foreach($company_records as $company){
            $temp["id"]     = $company->getId();
            $temp["name"]   = $company->getName();
            $temp["phone"]  = $company->getPhone();
            $temp["email"]  = $company->getEmail();
            $data["data"][] = $temp;
        }
        //converts data to JSON
        return new JsonResponse($data);

这些代码所做的只是将 JSON 响应返回到 Kendo UI 网格控件,以便它知道要显示的内容。 我循环company_records来创建我需要的结构。

我需要将这些过滤器动态地应用于 $company_records,而不是以某种方式静态地应用。 这可能吗?

资源:

以下是在所有示例中静态执行的操作

如何在 doctrine 2 实体的存储库中使用复杂的标准?

您可以使用"matching()"逐个应用条件:

$expr1 = Doctrine'Common'Collections'Criteria::expr();
$c1 = Doctrine'Common'Collections'Criteria::create();
$c1->where($expr->eq('fieldName', 'someValue'));
$company_total = $repository->matching($c1);
$expr2 = Doctrine'Common'Collections'Criteria::expr();
$c2 = Doctrine'Common'Collections'Criteria::create();
$c2->where($expr2->eq('fieldName2', 'someValue2'));
$company_total2 = $company_total->matching($c2);