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?