How to search with an array in Yii2 search model

Tsz Fung Li  picture Tsz Fung Li · Jan 30, 2017 · Viewed 10k times · Source
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?

Answer

Nana Partykar picture Nana Partykar · Jan 30, 2017

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;
  }  

}