I'd like to filter my items results in Cakephp 3, if the multiple rules are set. For example before filtering result is all items list. If the category is set, the result is all items in that category. if the price is set all items in that category with that price
$item = $this->Items->find('all',['conditions' =>
IF ($category) RETURN Items.in.Category ELSE RETURN ALL,
IF ($price) RETURN Items.in.Price ELSE RETURN ALL,
]);
any idea?
You can do something like this,
$condition=[]; //Declaration of array for conditions
if(isset($category})
$condition[] = ['category ' => $category];
if($price)
$condition[] = ['price ' => $price];
And Now use this, in your find(condition)
$this->Items->find('all',['conditions' => ['AND'=>[$condition]]];
Note: - Use AND if both the variables are set, you want common results otherwise use OR instead of AND.
Hope This will help you.