Answer the question
In order to leave comments, you need to log in
Why is ActiveQuery needed in Yii2?
I have been working with Yii2 for more than 3 years, all this time I managed exclusively with ActiveRecord. But there is also ActiveQuery, but even hands never reached it.
Why exactly is this class needed?
To be more specific, why is this option needed when generating a model through Gii?
Answer the question
In order to leave comments, you need to log in
To be more specific, why is this option needed when generating a model through Gii?
So that you can add your custom methods or override native ones (all(), one() and others, in short, override native ActiveQuery and change to your own abstractions)
Here is an example of a controller:
Ну раз нужен -- добавляем такой методclass TrainingQuery extends \yii\db\ActiveQuery { public function actual($date) { return $this->alias('t') ->joinWith(['lesson l' => function($q){ $q->joinWith('studio s'); }]) ->andWhere(['l.active' => 1]) ->andWhere(['s.active' => 1]) ->andWhere(['t.date' => $date]) ->all(); } }
В сущности перенацеливаем родной find() на мой кастомныйclass Training extends \yii\db\ActiveRecord { ... public static function find() { return new \mynamespace\entities\query\TrainingQuery(get_called_class()); }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question