我需要搜索一个范围的字段.如何在搜索模型中应用介于或大于/小于语句的语句.
$params['MlsSearch']['min_price'] = 10; $params['MlsSearch']['max_price'] = 100; $searchModel = new ModelSearch(); $dataProvider = $searchModel->search($params);
1)在模型中定义属性.
原文链接:https://www.f2er.com/php/138503.htmlclass ModelSearch extends Model { public $min_price; public $max_price; /*....*/ }
2)使属性安全
public function rules() { return [ /*... */ [['min_price','max_price',],'safe'],] }
public function search($params) { /*... */ $query->andFilterWhere(['>','price',$this->min_price]); $query->andFilterWhere(['<',$this->max_price]); /*... */ }