Reputation: 67
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?
Upvotes: 3
Views: 11954
Reputation: 925
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
Upvotes: 0
Reputation: 10548
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;
}
}
Upvotes: 4
Reputation: 821
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,
...
]
);
Upvotes: 2