gridview with junction table relation function

How are you all!

Little stuck here:

I have a model that has a function to get all the guardians linked to a student via a junction table:


    public function getLinkedGuardians()

    {

        return $this->hasMany(Guardian::className(), ['id' => 'guardianID'])

         ->viaTable(GuardianStudentLink::tableName(), ['studentID' =>'id']);

    }

In my Student view I am trying to output a gridview of all the guardians linked to that student.

With the code below I get an error:


 $dataProvider = new ActiveDataProvider([

             'query' => $model->linkedGuardians

            ]);


            echo GridView::widget([

            'dataProvider' => $dataProvider,

            'filterModel' => $searchModel,

            'columns' => [

                ['class' => 'yii\grid\SerialColumn'],


                'id',

                'title',

                'firstName',

                'secondName',

                'surname',

                // 'dateOfBirth',

                // 'dateTimeCreated',

                // 'notes',


                ['class' => 'yii\grid\ActionColumn'],

            ],

            ]);

The error says:

Invalid Configuration – yii\base\InvalidConfigException

The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.

I know that the $model->linkedGuardians is being put into the query and this is incorrect as I should be using a class that implements QueryInterface.

So how can I change the code above to get the correct class to $dataprovider, so I can display all the guardians?

Thank you in advance!

I am not sure if this is the best way to do this, but this seemed to work:


$dataProvider = new ActiveDataProvider([

 'query' => $model->hasMany(Guardian::className(), ['id' => 'guardianID'])

->viaTable(GuardianStudentLink::tableName(), ['studentID' =>'id'])

]);


echo GridView::widget([

'dataProvider' => $dataProvider,

'filterModel' => $searchModel,

'columns' => [

	['class' => 'yii\grid\SerialColumn'],


	'id',

	'title',

	'firstName',

	'secondName',

	'surname',

	// 'dateOfBirth',

	// 'dateTimeCreated',

	// 'notes',

],

]);






$dataProvider = new ActiveDataProvider([

  'query' => $model->getLinkedGuardians()

]);






$model->linkedGuardians



Will execute the query. ActiveDataProvider needs a query

I could have sworn I had tried that! LOL.

OK well that works great!

Thank you Timmy78