T
T
tkirsan4ik2017-01-18 18:26:21
Yii
tkirsan4ik, 2017-01-18 18:26:21

Yii2. How to override the all() method in an ActiveQuery model?

Hello everybody.
Faced the following problem:
the table has an active field, you need to select only those rows in which the active=1 field is absolutely for all calls to this model.
The solution that I found does not quite suit me (since you have to change this in all calls, and there are a lot of them, and you need to do this for more than one model):

public function allActive(){
$this->andWhere('table_name.active=1')->all();
}

And all attempts to write this construction in all() led to errors
. Is it possible to override the all() method and insert the active=1 condition there?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-01-18
@tkirsan4ik

And all attempts to write this construction in all() led to errors

don't do that, it's bad practice. in fact, you need to implement the default scope. There are many approaches for this, some of them can be gleaned, for example, from this article on Habré

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question