dongzhong2674 2016-01-27 11:33
浏览 58

学说一次设置多个标准,而不是一次设置多个标准

I'm using the KendoUI Framework GRID which returns back JSON of the users current filter settings.

Its result looks something like this for the filters.

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

I would need to iterate over this array and create criteria for doctrine to filter by. Every sample I've seen though shows it all happening together all at once which won't work with my scenario.

I'm also using Symfony 3.0, so the code below creates the repository.

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

Then I do this for the rest of the code currently.

$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);

All this code is doing is returning a JSON response to the Kendo UI Grid Control so it knows what to display. I loop on company_records to create the structure I need.

I need to apply those filters to $company_records dynamically and not statically somehow. Is this possible?

Resources:

Below is doing it statically in all examples

How do I use a complex criteria inside a doctrine 2 entity's repository?

  • 写回答

1条回答 默认 最新

  • dro80463 2018-10-02 09:27
    关注

    You can apply criterias one by one using "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);
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮