我需要搜索字段的范围。如何在搜索模型中应用介于或大于/小于的语句。
类似于此的东西。但是,这些属性在搜索模型中无效
$params['MlsSearch']['min_price'] = 10;
$params['MlsSearch']['max_price'] = 100;
$searchModel = new ModelSearch();
$dataProvider = $searchModel->search($params);
答案 0 :(得分:18)
1)在模型中定义属性。
class ModelSearch extends Model
{
public $min_price;
public $max_price;
/*....*/
}
2)确保属性安全
public function rules()
{
return [
/*... */
[['min_price', 'max_price', ], 'safe'],
]
}
3)修改搜索功能
public function search($params)
{
/*... */
$query->andFilterWhere(['>', 'price', $this->min_price]);
$query->andFilterWhere(['<', 'price', $this->max_price]);
/*... */
}
答案 1 :(得分:7)
您也可以使用BETWEEN构造:
$query->andFilterWhere(['between', 'price', $this->min_price, $this->max_price]);