Yii Framework Forum: Activedataprovider Query Property On Relations - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Activedataprovider Query Property On Relations

#1 User is offline   desis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 24-August 13

Posted 09 June 2014 - 11:05 AM

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(), ] );
}
0

#2 User is offline   desis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 24-August 13

Posted 10 June 2014 - 10:48 AM

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.
0

#3 User is offline   bis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 27-May 14

Posted 10 June 2014 - 07:52 PM

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:

Quote

Note: A relation method returns an instance of yii\db\ActiveQuery. If you access the relation like an attribute (i.e. a class property), the return value will be the query result of the relation, which could be an instance of yii\db\ActiveRecord, an array of that, or null, depending the multiplicity of the relation. For example, $customer->getOrders() returns an ActiveQuery instance, while $customer->orders returns an array of Order objects (or an empty array if the query results in nothing).

0

#4 User is offline   Ahamed Rifaideen 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 380
  • Joined: 14-February 13
  • Location:chennai, tamilnadu

Posted 11 June 2014 - 12:42 AM

View Postbis, on 10 June 2014 - 07:52 PM, said:

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.
My Blog - Yii Learner
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users