Answer the question
In order to leave comments, you need to log in
How to properly use ActiveQuery extending ActiveRecord?
I have a User class that inherits from ActiveRecord
In this class, I extend the find method like this
public static function find()
{
return new UserQuery(get_called_class());
}
class UserQuery extends ActiveQuery
{
/**
* @param $role integer
* @return $this
*/
public function forRole($role)
{
return $this->andWhere(['role' => $role]);
}
}
User::find()->select(['username','id'])->forRole(User::ROLE_PROVIDER)->indexBy('id')->column()
Answer the question
In order to leave comments, you need to log in
It's better to make a getUserProviders method in the User class, and put your selection code there.
This is more correct, because this selection can be used in many places, and if it needs to be changed, it is easier to do it in one place than to climb around the entire project.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question