Yii在逗号分隔的查询字符串中搜索每个术语不能按预期工作

Yii在逗号分隔的查询字符串中搜索每个术语不能按预期工作

问题描述:

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.

我想通过搜索用逗号分隔的查询字符串中的每个术语来完成搜索我的模型。 例如,如果字符串是“matza,red wine” code>,那么我想搜索匹配“matza” code>和“red%20wine” code的项目 >。 这是我正在使用的代码: p>

  $ 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); 
} \  n 
 $ results = FoodItem :: model() - > findAll($ criteria); 
  code>  pre> 
 
 

它运行全部,但结果不正确 。 例如,我希望搜索“A,B” code>会产生与“B,A” code>相同的结果,但事实并非如此。 我希望有一种方法可以记录或 echo code>生成的 $ criteria code>的一些表示。 我觉得它与我使用 mergeWith code>的方式有关。 p>

请让我知道我哪里出错了。 p> \ n div>

tCriteria seems to be empty at the end of the loop. You are not merging anything.

Maybe you meant to make the two addSearchCondition lines work on tCriteria whereas you have them working on criteria.