# Cakephp 3.X complex query builder

**URL:** <https://discourse.cakephp.org/t/cakephp-3-x-complex-query-builder/3055>\
**Category:** Need Help\
**Created:** [August 20, 2017, 2:06am UTC](https://discourse.cakephp.org/t/cakephp-3-x-complex-query-builder/3055 "2017-08-20T02:06:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yomi](https://avatars.discourse-cdn.com/v4/letter/y/96bed5/32.png) [@Yomi](https://discourse.cakephp.org/u/Yomi)\
**Post date:** [August 20, 2017, 2:06am UTC](https://discourse.cakephp.org/t/cakephp-3-x-complex-query-builder/3055/1 "2017-08-20T02:06:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [August 20, 2017, 2:09am UTC](https://discourse.cakephp.org/t/cakephp-3-x-complex-query-builder/3055/2 "2017-08-20T02:09:29Z")

</div>

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

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

```
