I have several parameters to search for, including dropDownList .

Example:

  <?= $form->field($model, 'maincatalog')->dropDownList($maincatalog, ["prompt" => Yii::t('app', 'select_cat')])->label(false) ?> <?= $form->field($model, 'listproduct')->dropDownList($listproduct, ["prompt" => Yii::t('app', 'select_cat')])->label(false) ?> <?= $form->field($model, 'q')->textInput(['class' => 'input'])->label('')?> $query = Addproduct::find() ->where(['catalog' => $maincatalog]) ->andWhere(['product' => $listproduct]) ->andWhere(['like', 'titleru', $q])->all(); 

If $maincatalog not selected, then $maincatalog does not matter, because of this, the query does not work, that is, returns null, but if you put conditions on them a lot.
How can I ignore where(['catalog' => $maincatalog]) if $maincatalog stores a $maincatalog value?

  $query = Addproduct::find(); if ($maincatalog != NULL) $query->where(['catalog' => $maincatalog]); if ($listproduct != NULL) $query->andWhere(['product' => $listproduct]); $query->andWhere(['like', 'titleru', $q])->all(); 

this code is not working, why?

You see, the condition if if ($ maincatalog! = NULL) can have a lot of combinations, because of this I get a lot of type conditions so

 if($maincatalog == "" && $listproduct == "" && $q == ""){ $query = "all empty"; } elseif($maincatalog == "" && $listproduct != "" && $q == ""){ $query = Addproduct::find()->where(['product' => $listproduct]) ->andWhere(['like', 'titleru', $q])->all(); } elseif($maincatalog != "" && $listproduct == "" && $q == "") { $query = Addproduct::find()->where(['catalog' => $maincatalog]) ->andWhere(['like', 'titleru', $q])->all(); } elseif($maincatalog == "" && $listproduct == "" && $q != "") { $query = Addproduct::find()->where(['like', 'titleru', $q])->all(); } elseif($maincatalog == "" && $listproduct != "" && $q != "") { $query = Addproduct::find()->where(['product' => $listproduct]) ->andWhere(['like', 'titleru', $q])->all(); } elseif($maincatalog != "" && $listproduct == "" && $q != "") { $query = Addproduct::find()->where(['catalog' => $maincatalog]) ->andWhere(['like', 'titleru', $q])->all(); } else { $query = Addproduct::find()->where(['catalog' => $maincatalog]) ->andWhere(['product' => $listproduct])->andWhere(['like', 'titleru', $q])->all(); } 

In the future, I want to add another dropdown, because of this condition, it will become impossible to describe all. How to cope with a single request without a condition even if the parameters are empty?


understand the condition if if ($ maincatalog! = NULL) can have a lot of combinations because of this I get a lot of type conditions so

 if($maincatalog == "" && $listproduct == "" && $q == ""){ $query = "all empty"; } elseif($maincatalog == "" && $listproduct != "" && $q == ""){ $query = Addproduct::find()->where(['product' => $listproduct])- >andWhere(['like', 'titleru', $q])->all(); } elseif($maincatalog != "" && $listproduct == "" && $q == "") { $query = Addproduct::find()->where(['catalog' => $maincatalog])- >andWhere(['like', 'titleru', $q])->all(); } elseif($maincatalog == "" && $listproduct == "" && $q != "") { $query = Addproduct::find()->where(['like', 'titleru', $q])->all(); } elseif($maincatalog == "" && $listproduct != "" && $q != "") { $query = Addproduct::find()->where(['product' => $listproduct])->andWhere(['like', 'titleru', $q])->all(); } elseif($maincatalog != "" && $listproduct == "" && $q != "") { $query = Addproduct::find()->where(['catalog' => $maincatalog])- >andWhere(['like', 'titleru', $q])->all(); } else { $query = Addproduct::find()->where(['catalog' => $maincatalog])- >andWhere(['product' => $listproduct])->andWhere(['like', 'titleru', $q])- >all(); } 

In the future, I want another dropdown to add because of this condition, it will be impossible to describe everything yet. How to cope with one request without a condition even if the parameters are empty?


This is a model to search.

namespace common \ models; use yii \ base \ Model;

class SearchForm extends Model {

 public $q; public $listproduct; public $maincatalog; public function rules() { return [ ['q', 'string'], ['listproduct', 'string'], ['maincatalog', 'string'] ]; } 

}


Yii2 ActiveRecord How to break a query as needed - Yii / Yii2 Such a query $ query = Addproduct :: find () -> where (['tip' => $ tip]) -> andWhere (['product' => $ listproduct]) -> andWhere (['like', 'titleru', $ q]) -> all ();

is it possible to break something like this: $ query = Addproduct :: find ();

if ($ maincatalog! = NULL) $ query-> where (['catalog' => $ maincatalog]);

if ($ listproduct! = NULL) $ query-> andWhere (['product' => $ listproduct]);

$ query-> andWhere (['like', 'titleru', $ q]) -> all ();

or through andFilterWhere so:

$ query = Addproduct :: find () -> where ('***');

$ query-> andFilterWhere (['catalog' => $ maincatalog]);

$ query-> all (); I tried both options but does not work


  $query = Addproduct::find(); if ($maincatalog != NULL) $query->where(['catalog' => $maincatalog]); if ($listproduct != NULL) $query->andWhere(['product' => $listproduct]); $query->andWhere(['like', 'titleru', $q])->all(); 

this code is not working why?

  • Good morning. Request for search model? If so, show the complete model code. - user216615
  • Add to the question all your "answers" with the edit button, if they relate to the current question, if not - ask a new question. - 0xdb
  • You have not shown the full code of the model and controller. What is hard to do? - user216615
  • @slo_nik If you transfer something to a question from the outside, then indicate this explicitly in the comment to the edit. - 0xdb

2 answers 2

It is not necessary to write the request in one line, you can split it into several lines by adding the conditions you need. The state of $ query from this does not "reset"

 $query = Addproduct::find(); if ( $maincatalog != NULL ) $query->where(['catalog' => $maincatalog]); $query->andWhere(['product' => $listproduct]) ->andWhere(['like', 'titleru', $q])->all(); 
  • I have 3 parameters here, what is your way to split the request? $ q, $ listproduct, $ maincatalog - Proger
  • @Proger is the same as $ maincatalog - Peresada

If you want to organize a search on a table, from your question it is not completely clear, then simply generate a search model using gii.

There is such a thing as yii \ db \ ActiveQuery :: andFilterWhere () . So, this public method adds an additional WHERE condition to the existing one, but ignores empty operands.

Based on this, your request turns into the following.

 $query = Addproduct::find()->where('***'); $query->andFilterWhere(['catalog' => $maincatalog]); $query->all(); 

Something like that.