Joshi
Joshi

Reputation: 2790

Yii2 - Filtered Data In Grid View

I have a test set-up which involves many to many relation.

Students Model
id
name

Parents Model
id
name

Students_Parents Model
student_id
parent_id

What I am trying to do is redirecting student update form to parent grid view. I want to show the related students parent records only in the parents grid view.

for example mysql query to show the related record is like this:

SELECT s.id, s.name, p.id, p.name
FROM `students` s, parents p, student_parent sp
WHERE s.id = sp.student_id
AND p.id = sp.parent_id
AND s.id =102

and in the controller I could find the same record like this when I use the data of student with the id of 102

$model2 = Students_Parents::find()
            ->where(['student_id' => $model->id])
            ->all();

my redirect code in controller I am trying to do is:

if(isset($_POST['parents'])){
                   return $this->redirect(['parents/index','id'=>$model2]);
            }

Here the form is redirecting correctly, but no filter is applied. How can I incorporate the query in redirect so I get the related data only in the GridView.

Thanks

Upvotes: 0

Views: 526

Answers (1)

Adam Fentosi
Adam Fentosi

Reputation: 1268

You have to define in the redirect the name of the filter input as the key, like:

    $this->redirect(['parents/index','StudentSearch[id]'=>2]);

Upvotes: 3

Related Questions