# Building a "matching" query with ORs

**URL:** <https://discourse.cakephp.org/t/building-a-matching-query-with-ors/2796>\
**Category:** Need Help\
**Created:** [June 28, 2017, 3:59pm UTC](https://discourse.cakephp.org/t/building-a-matching-query-with-ors/2796 "2017-06-28T15:59:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mspeener](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mspeener](https://discourse.cakephp.org/u/mspeener)\
**Post date:** [June 28, 2017, 3:59pm UTC](https://discourse.cakephp.org/t/building-a-matching-query-with-ors/2796/1 "2017-06-28T15:59:15Z")

</div>

I have a search query that uses matching. The main table is called Transactions and it has a belongsToMany association with table Customers, aliased as Clients, through TransactionsCustomers. I’m merely trying to match a string that could be in two different fields.

Example:

> $query-\>matching(“Clients”, function($q) use($clients) {  
> return $q-\>where([“Clients.C\_CustomerName like ‘%$clients%’”]);  
> });

This works from an produces SQL like this:  
INNER JOIN customers Clients ON (Clients.C\_CustomerName like ‘%houser%’ AND Clients.C\_IDCustomer = (TransactionsCustomers.customer\_id))

but when I add an orWhere like so:

> $query-\>matching(“Clients”, function($q) use($clients) {  
> return $q-\>where([“Clients.C\_CustomerName like ‘%$clients%’”])  
> -\>orWhere([“Clients.C\_EntityName like ‘%$clients%’”]);  
> });

Or just put them both on the same line:

> $query-\>matching(“Clients”, function($q) use($clients) {  
> return $q-\>where([“Clients.C\_CustomerName like ‘%$clients%’ OR Clients.C\_EntityName like ‘%$clients%’”]);  
> });

I get SQL like this:

INNER JOIN customers Clients ON  
(Clients.C\_CustomerName like ‘%houser%’ OR Clients.C\_EntityName like ‘%houser%’ OR Clients.C\_IDCustomer = (TransactionsCustomers.customer\_id))

The problem is that this is all ORs which matches everything. It going to return every transaction with a Client associated with it.

How do I code this query so it works properly? It probably should produce SQL something like:

INNER JOIN customers Clients ON  
((Clients.C\_CustomerName like ‘%houser%’ OR Clients.C\_EntityName like ‘%houser%’ ) AND Clients.C\_IDCustomer = (TransactionsCustomers.customer\_id))

---

<div class="post-metadata">

**Author:** ![xaphalanx](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/xaphalanx/32/575_2.png) [@xaphalanx](https://discourse.cakephp.org/u/xaphalanx)\
**Post date:** [June 28, 2017, 4:13pm UTC](https://discourse.cakephp.org/t/building-a-matching-query-with-ors/2796/2 "2017-06-28T16:13:49Z")

</div>

I could be wrong but I think you want to do something like this:

$query = $articles-\>find()  
-\>where([‘title LIKE’ =\> ‘%First%’])  
-\>andWhere(function ($exp) {  
return $exp-\>or\_([  
‘author\_id’ =\> 2,  
‘is\_highlighted’ =\> true  
]);  
});

---

<div class="post-metadata">

**Author:** ![mspeener](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mspeener](https://discourse.cakephp.org/u/mspeener)\
**Post date:** [June 28, 2017, 4:24pm UTC](https://discourse.cakephp.org/t/building-a-matching-query-with-ors/2796/3 "2017-06-28T16:24:33Z")

</div>

I think typing it out made it all make sense.

Here’s what worked:

> $query-\>matching(“Clients”, function($q) use($clients) {  
> return $q-\>where([“(Clients.C\_CustomerName like ‘%$clients%’ OR Clients.C\_EntityName like ‘%$clients%’)”]);  
> });

I had to add parenthesis into the query string so it would work properly.
