I have my User class extending ActiveRecord and implementing IdentityInterface and loading it into the Yii::$app->user component.
When I define a relationship in my class like this:
public function getRole()
{
return $this->hasOne(UserRoleRecord::className(), ['id' => 'userroleId']);
}
Everytime I run a page load the following Query will be executed:
SELECT * FROM userroles WHERE id = X
This is with me not running Yii::$app->user->identity->role
I’ve modified my “findIdentity()” function to make the join directly to avoid running this extra Query.
return static::find()->where(['users.id' => $id])->joinWith('role')->limit(1)->one();
But the relationship Query seem to run every time anyways. Is there a way around this (without removing the relationship "hasOne" method of course and just run the joinWith())? I would be cool for consitency to keep all relationships defined in my Model. But if I have some Models with a couple of relationships the Queries keep adding up.
Any ideas? Cheers