I have models Section, Course, Term, Categories (and others).
A Section is a particular Course in a particular Term.
Categories belong to a section.
So, on my categories index view I wish to filter by a particular term and/or course.
My CategorySearch Model (removed unnecessary code)
public function rules()
{
return [
[['category_id', 'section_id', 'category_weight', 'category_ord'], 'integer'],
[['category_name', 'section','term', 'course'], 'safe'],
];
}
public function search($params)
{
$session = Yii::$app->session;
$query = Category::find()
->joinWith(['section','section.term','section.course'])
->where(['course.teacher_id' => $session['user.user_id']])
->andWhere(['course.school_year_id' => $session['schoolYears.currentSchoolYear']])
->orderBy('section_id','category_name');//'section.term_id','section.course_id',
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['course'] = [
'asc' => ['section.course.course_name' => SORT_ASC],
'desc' => ['section.course.course_name' => SORT_DESC],
];
$dataProvider->sort->attributes['term'] = [
'asc' => ['section.term.term_name' => SORT_ASC],
'desc' => ['section.term.term_name' => SORT_DESC],
];
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'category_id' => $this->category_id,
'section_id' => $this->section_id,
'category_weight' => $this->category_weight,
'category_ord' => $this->category_ord,
]);
$query->andFilterWhere(['like', 'category_name', $this->category_name])
->andFilterWhere(['like','course.course_name', $this->section]);
return $dataProvider;
}
I can’t wrap my head around how to get the nested relations in the filter to work. Any help would be appreciated.
–
J