# Remove where condition from query

**URL:** <https://discourse.cakephp.org/t/remove-where-condition-from-query/1900>\
**Category:** Need Help\
**Created:** [January 20, 2017, 2:02pm UTC](https://discourse.cakephp.org/t/remove-where-condition-from-query/1900 "2017-01-20T14:02:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandinjr](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sandinjr](https://discourse.cakephp.org/u/sandinjr)\
**Post date:** [January 20, 2017, 2:02pm UTC](https://discourse.cakephp.org/t/remove-where-condition-from-query/1900/1 "2017-01-20T14:02:26Z")

</div>

My app receives an identifier that could match one of two fields in a table. Rather than to blindly check for a match on either, I would like to modify the query in the Table’s beforeFind function. If the identifier looks like a username, I’ll leave the query alone. If it looks like the alternate field, I want to remove the username condition from the query and add the alternate condition. I don’t see any way to change or remove a where condition from a query, though. Am I missing something?

---

<div class="post-metadata">

**Author:** ![markstory](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/markstory/32/182_2.png) [@markstory](https://discourse.cakephp.org/u/markstory)\
**Post date:** [January 21, 2017, 5:09am UTC](https://discourse.cakephp.org/t/remove-where-condition-from-query/1900/2 "2017-01-21T05:09:46Z")

</div>

The `$query->where()` method has an `$overwrite` parameter which lets you replace all of the conditions with new ones. This might let you do what you want if you need to replace the conditions entirely.

---

<div class="post-metadata">

**Author:** ![sandinjr](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sandinjr](https://discourse.cakephp.org/u/sandinjr)\
**Post date:** [January 23, 2017, 1:25pm UTC](https://discourse.cakephp.org/t/remove-where-condition-from-query/1900/3 "2017-01-23T13:25:16Z")

</div>

@markstory: I did find that in the source, but $overwrite doesn’t change  
the placeholders in the SQL, so while I can replace or remove one or  
more parameter values, I cannot remove it entirely from the SQL.  
Removing a parameter value without removing the placeholder from the SQL  
of course generates an exception.

I have found a workaround in my app to remove this need, but I’m still  
curious about this question.
