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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序