I use ActiveController as written in the documentation.

public function actionIndex() { $modelClass = $this->modelClass; $developer = Developers::findOne([ 'id' => Yii::$app->request->get('developer_id'), ]); $complex_type = ComplexType::findOne([ 'id' => Yii::$app->request->get('complex_id'), ]); $complex = Complex::findOne([ 'type_id' => $complex_type->id, 'developer_id' => $developer->id, ]); $query = $modelClass::find() ->where( [ 'amount_room' => Yii::$app->request->get('amount_room'), 'yardage' => Yii::$app->request->get('yardage'), 'level' => Yii::$app->request->get('level'), 'complex_id' => $complex->id, ]); return new ActiveDataProvider([ 'query' => $query, ]); } 

The question is as follows: Now it is necessary to transfer all the parameters, which is logical in principle. What is the way to make a check for the parameter fullness, and already depending on it do the search? I can do a bunch of ifs, but it’s not very nice. How best to implement this?

    1 answer 1

    You can use filterWhere, andFilterWhere and orFilterWhere, if the specified value is empty, then the condition will be ignored.

     // $username и $email вводит пользователь $query->filterWhere([ 'username' => $username, 'email' => $email, ]); 

    More details can be found here https://github.com/yiisoft/yii2/blob/master/docs/guide-ru/db-query-builder.md#Conditions- for- filters-

    • Yes, I just started digging this way. Apparently inadvertently did not see this method. Thanks - edvardpotter