I would like to accomplish searching my model by searching for each term in a query string separated by commas. For example, if the string is "matza,red wine"
then I want to search for items matching "matza"
and "red%20wine"
. This is the code I am using:
$qString = $_GET['q'];
$criteria = new CDbCriteria();
$queryTerms = explode(',', $qString);
foreach ($queryTerms as $q) {
$tCriteria = new CDbCriteria();
$criteria->addSearchCondition('name', $q, true);
$criteria->addSearchCondition('text_ingredients', $q, true, 'OR');
$criteria->mergeWith($tCriteria);
}
$results = FoodItem::model()->findAll($criteria);
And it runs and all, but the results are not correct. For example, I would expect that searching for "A,B"
would yield the same results as for "B,A"
, but it does not. I wish there was a way to log or echo
some representation of the resulting $criteria
. I have a feeling it has something to do with the way I am using mergeWith
.
Please let me know where I am going wrong.