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 & 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 & userMaxAge).
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-
userMinAge <= min_age <= userMaxAge OR
userMinAge <= max_age <= userMaxAge
I'm trying to run this using Yii's CDbCriteria and have come up with the following-
$criteria->condition = "$minAge <= min_age <= $maxAge OR $minAge <= max_age <= $maxAge";
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.
Can anyone spot why this approach isn't working? Are there any mistakes with my use of CDbCriteria?
Many thanks,
Goose