title: CDbCriteria - modelling two inequality conditions - Yii
body: <p>I need to run a query that returns products that match age requirements given by a user. Products in the database are assigned a minimum and maximum age that apply (min_age &amp; max_age). The user will select an age range they wish to search under (for the sake of this question let's call this the userMinAge &amp; userMaxAge).</p>
  
  <p>Since I want to return any product where there is some cross over between the user's selected age range and the age range stored against the product, I think this works out as needing to satisfy just one of the following inequalities-</p>
  
  <p>userMinAge &lt;= min_age &lt;= userMaxAge OR </p>
  
  <p>userMinAge &lt;= max_age &lt;= userMaxAge</p>
  
  <p>I'm trying to run this using Yii's CDbCriteria and have come up with the following-</p>
  
  <pre><code>$criteria-&gt;condition = "$minAge &lt;= min_age &lt;= $maxAge OR $minAge &lt;= max_age &lt;= $maxAge";
  </code></pre>
  
  <p>This runs OK (as in no errors are thrown), but does not exclude products that it should. i.e a product with age range 8-19 was included when the user selected ages 20-35. Something is wrong, but my fairly average understanding of CDbCriteria is baffling me somewhat.</p>
  
  <p>Can anyone spot why this approach isn't working? Are there any mistakes with my use of CDbCriteria?</p>
  
  <p>Many thanks,</p>
  
  <p>Goose</p>