1

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?

Community
  • 1
  • 1
Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154

1 Answers1

1

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