Activedataprovider Query Property On Relations

I am trying to use the ActiveDataProvider with a Model that has a one to many relation. I created the Model using Gii and it created a get method for the relation that I needed.

But when I used this property in the query parameter of the ActiveDataProvider, I got an error:

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

I later changed my code so that instead of referencing the relation via the property, I used the created get method and this worked for me. I was just wondering if this is the desired behavior, and if so, why I shouldnt use the property.

For example

class Model

{

* @property MyRelation[] $myRelations


getMyRelations(){return $this->hasMany(MyRelation::className(), ['xid' => 'id']);};

}

class Test

{

// This fails


$data = new ActiveDataProvider( ['query' => $model->myRelations, ] );


// This Succeeds


$data = new ActiveDataProvider( ['query' => $model->getMyRelations(), ] );

}

I think I discovered my answer. Mostly this is due to my lack of php experience, but I had assumed the property name I was accessing is the same as the method with ‘get’ prefixed.

I don’t believe this is the case. In my case, when I thought I could use MyRelations or getMyRelations() because I thought they were the same, I was mistaken. Calling MyRelations returns the ActiveRecord of the result from the query produced by getMyRelations(). Calling getMyRelations() returns the query and that is what is needed by the DataProvider.

Nope, your php experience is not an issue here I’m afaid. It would be the expected behaviour, and I find it rather disturbing when an another getter turns out to be a blackbox.

From the guide:

you are right bis.