drfu80954
2017-01-30 09:37
浏览 413
已采纳

如何在Yii2搜索模型中使用数组进行搜索

public function actionIndex()
{
    $searchModel = new SubjectSearch();
    $searchModel->search()->query->andFilterWhere(['in','subjectID',[1,2,3]]);
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

I've tried different ways like

$searchModel->subjectID = [1,2,3]

$searchModel->search()->query->andFilterWhere(['in','subjectID',[1,2,3]]);

But they doesn't work for me.

Actually, ArrayDataprovider might seem to be a better solution in this case, but Array Dataprovide won't work with filters.

BTW, the real question is to search with ManyToMany Relationship.

many Users to many Groups.
many Groups to many Subjects.

UserGroupTbl contains UserID and GroupID
SubjectGroup contains SubjectID and GroupID

I'm trying to do that with:

$groups = $appUser->getGroups();
$subjectIDs = [];
foreach ($groups as $group) {
    $subjectIDs[] = $group->getSubjectIDs
}
$searchModel = new SubjectSearch();
$searchModel->subjectID = $subjectIDs;

But that doesn't work and is certainly not a good method

Please help me a little bit with it.

================Update==============

    $searchModel = new SubjectSearch();
    $searchModel->subjectID = [1,2];
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

Result in "Array to string conversion" error.

    $searchModel = new SubjectSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    $dataProvider->query->andFilterWhere(['in','subjectID',[1,2]]);;

This method actually worked. BTW, do you have a little bit advice about handling many to many searching?

图片转代码服务由CSDN问答提供 功能建议

  public function actionIndex()
 {
 $ searchModel = new SubjectSearch(); 
 $  searchModel-> search() - > query-> andFilterWhere(['in','subjectID',[1,2,3]]); 
 $ dataProvider = $ searchModel-> search(Yii ::  $ app-> request-> queryParams); 
 
返回$ this-> render('index',[
'searchModel'=> $ searchModel,
'dataProvider'=> $ dataProvider  ,
]); 
} 
   
 
 

我尝试了不同的方法,例如

   $ searchModel-> subjectID = [1,2,3] 
 
 $ searchModel-> search() - > query-> andFilterWhere(['in','subjectID',[1,2,3]  ]]); 
   
 
 

但它们对我不起作用。

实际上,ArrayDataprovider似乎更好 在这种情况下解决方案,但Array Dataprovide不适用于过滤器。

BTW,真正的问题是使用ManyToMany关系进行搜索。

许多 用户到多个组。
many组到多个主题。

UserG roupTbl包含UserID和GroupID
SubjectGroup包含SubjectID和GroupID

我正在尝试使用:

  $ groups  = $ appUser-> getGroups(); 
 $ subjectIDs = []; 
foreach($ groups as $ group){
 $ subjectIDs [] = $ group-> getSubjectIDs 
} 
 $ searchModel = new  SubjectSearch(); 
 $ searchModel-> subjectID = $ subjectIDs; 
   
 
 

但这不起作用,当然不是一个好方法

请帮我一点。

================更新====== ========

  $ searchModel = new SubjectSearch(); 
 $ searchModel-> subjectID = [1,2]; 
 $ dataProvider =  $ searchModel-> search(Yii :: $ app-> request-> queryParams); 
   
 
 

导致“数组到字符串转换”错误。< / p>

  $ searchModel = new SubjectSearch(); 
 $ dataProvider = $ searchModel-&gt; search(Yii :: $ app-&gt; request-&gt; queryParams); 
  $ dataProvider-&GT;查询 - &GT; andFilterWhere([ '中', 'subjectID',[1,2]]);; 
 <  / code>  
 
 

这种方法确实有用。 BTW,您对处理多对多搜索有什么建议吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doulang6013 2017-01-30 10:11
    已采纳

    Each time you call search you will get a new query object so you can't add parameters to it, use this method:

    $searchModel = new SubjectSearch();
    $searchModel->subjectID = [1,2,3];
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    

    In the SubjectSearch model you should have this in the search function:

    $query->andFilterWhere(
       [
          ...
          'subjectId' => $this->subjectID,
          ...
       ]
    );
    
    打赏 评论
  • dqxyh48864 2017-01-30 19:18

    You will not believe that today I was also stuck in same situation. Where I thought IN will work as how I wanted. But, strangely not worked for me.

    I tried.

    $dataProvider->query->andFilterWhere(['IN','subjectID', $this->subjectID]);
    

    In Yii Debugger, this query was changed to:

    SELECT * FROM tableName WHERE (condition) AND (subjectID = '1,2') ...

    Then, I Changed my query to

    $query->andFilterWhere(['subjectID' => $this->subjectID]);
    

    And, checked in Yii Debugger, the query was automatically changed to:

    SELECT * FROM tableName WHERE (condition) AND (subjectID IN ('1,2')) ...

    Which I was looking for.

    Updated Code

    I will suggest you to follow the code given below. It will work. @Ed209's Answer is right too.

    public function actionIndex(){
        $searchModel = new SubjectSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    

    SubjectSearch.php

    class SubjectSearch {
      .
      .
      .
    
      public function search($params) {
        $query = SubjectSearch::find();
    
        $this->load($params);
    
        if($this->subjectID != null){
          $query->andFilterWhere(['subjectID' => $this->subjectID]);
        }
    
        // You can add more clauses here to make your data more appropriate.
    
    
        $dataProvider = new ActiveDataProvider([
          'query' => $query,
          'pagination' => array('pageSize' => 20),
        ]);
    
        if (!$this->validate()) {
          return $dataProvider;
        }
        return $dataProvider;
      }  
    
    }
    
    打赏 评论
  • duankuaizhe8257 2017-01-31 06:08

    The best way i suggest you to use the following in which you don't need to add some extra condition like where or andWhere

        public function actionIndex()
    {
        $searchModel = new SubjectSearch();
        $queryData = Yii::$app->request->queryParams;
         $conditionData = [\yii\helpers\StringHelper::basename(($searchModel) => ['IN','subjectID'],[1,2,3]];
         $searchData = array_merge_recursive($queryData, $conditionData);
        $dataProvider = $searchModel->search($searchData);
    
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    

    This code works for me very smoothly. I hope it will help you too

    打赏 评论

相关推荐 更多相似问题