Yii2比较模型搜索中的条件

时间:2016-03-11 17:39:54

标签: yii2 yii2-basic-app

这里是我的搜索模型:

  $query->orFilterWhere(['like', 'name', $this->globalSearch])
        ->orFilterWhere(['like', 'content', $this->globalSearch])
        ->orFilterWhere(['like', 'address', $this->globalSearch]);


    $query->andFilterWhere(['like', 'name', $this->name])
        ->andFilterWhere(['like', 'address', $this->address])
        ->andFilterWhere(['like', 'price', $this->price])      
        ->andFilterWhere(['like', 'gender', $this->gender]);

我的搜索表单:

 <?php $form = ActiveForm::begin(['action' => ['results'], 'method' => 'get']); ?>
            <?= $form->field($searchModel, 'globalSearch') ?>  

            <?= $form->field($searchModel, 'gender')->dropDownList(array(''=>'-- Chọn đối tượng muốn tìm --', 'Nam'=>'Nam','Nữ'=>'Nữ', 'Không xác định' => 'Không xác định')) ?>
            <?= $form->field($searchModel, 'address')->dropDownList(array(''=>'-- TP --', 'Hà Nội'=>'Hà Nội','TP.HCM'=>'TP.HCM', 'Đà Nẵng' => 'Đà Nẵng')) ?>

            <div class="form-group">
             <div class="form-group">
    <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
    <?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>

我已经创建了一个供用户过滤的下拉列表。但是有一个“价格”字段,我想用“&gt; 200000”或“&lt; 200000”这样的值进行下拉。 无论如何都要将andFilterWhere(['like', 'price', $this->price])更改为可能'&gt; ='来比较值。

1 个答案:

答案 0 :(得分:1)

你应该使用

$query->andFilterWhere(['>=', 'price', $this->price]);

$query->andFilterWhere(['<=', 'price', $this->price]);

你也可以在&#34;之间使用&#34;如果你有两个值

$query->andFilterWhere(['between', 'price', 10000, 20000]);