Cakephp 3.X complex query builder

I am trying to build a query in cakephp 3.X with the following structure : A and B and C and [D or E] where A,B, C, D, E contains an array of values joined with OR conditions like

$schools = $schools->where([‘A IN’ => $arrayOfAOptions]),
$schools = $schools->where([‘B IN’ => $arrayOfBOptions]),
$schools = $schools->where([‘C IN’ => $arrayOfCOptions])

// want this to be firstly joined with OR before joining other
conditions with AND
$schools = $schools->where([‘D IN’ => $arrayOfCOptions])
//or
$schools = $schools->where([‘E IN’ => $arrayOfCOptions]

the problem here is that , I could not get fields D and E to be isolated as OR and join others with AND

in cakephp 2, i managed to achieve it with the following codes

$queryConditions = array(‘or’=> array_merge(array(‘School.D’ =>
$this->Session->read(‘conditions.D’)), array(‘School.E’ =>
$this->Session->read(‘conditions.E’))));

before merging it to the rest on the conditions Any help would be appreciated

In 3.x you can do the same way as 2.x

$schools = $schools->where([
    'OR' => [
        'D IN' => $arrayOfCOptions,
        'E IN' => $arrayOfEOptions,
    ],
]);
2 Likes