drfu80954
drfu80954
2017-01-30 09:37

如何在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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • doulang6013 doulang6013 4年前

    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,
          ...
       ]
    );
    
    点赞 评论 复制链接分享
  • duankuaizhe8257 duankuaizhe8257 4年前

    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

    点赞 评论 复制链接分享
  • dqxyh48864 dqxyh48864 4年前

    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;
      }  
    
    }
    
    点赞 评论 复制链接分享