Answer the question
In order to leave comments, you need to log in
Where to place filtering code in Yii2?
Good afternoon.
At the moment I am placing the search code for the model in one of the methods of the main table, in the same place I am tightening the links. Is it correct?
It turns out something like this:
<?php
namespace app\models;
class SomeModel extends ActiveRecord
{
// стандартные primaryKey, tableName
//-----
static function search($value1, $value2)
{
// тут какие то проверки значений $value1, $value2. Поиск по другим таблицам этих значений (что-то вроде проверки на существование
// в конце метода вывод
return $model->find()->with(['relation1', 'relation2'])->where(['value1' => $value1...])->all()
}
}
Answer the question
In order to leave comments, you need to log in
Of course in the models, where else.
If the question is whether it's right to pull links immediately with eager loading ( ->with(['relation1', 'relation2']) ), when filtering ( ->where(['value1' => $value1..]) ), then there are 2 answers, either yes or no)
If these relays are needed only to filter data by field from the relay, then no. It is better to use join for this.
If you still need this data from relays somewhere else, then yes, it’s better to immediately load them eagerly (as you did), so as not to make N extra sql queries later, where N is the number of sample rows.
Filters are ...
But as far as I know, logic can be described in models, and validation is also there. The filtering itself, the link to which was given above, is described in the controllers. Just read carefully. Links are naturally tightened in models.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question