我遇到了问题,我想知道这是否是在yii2中编写BETWEEN查询的正确方法。
在我的控制器代码中,我声明了$to_price
,这是我的SearchModel代码,但我总是得到null
的{{1}}值,以下是我的$this->to_price
代码
search model
如果它不是我得不到任何结果但我可以看到生成的命令在 public function search($params) {
$query = Events::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->joinWith(['eventsImages']);
$query->andFilterWhere([
'id' => $this->id,
'main_category_id' => $this->main_category_id,
'address' => $this->address,
]);
//$to_price = $params->to_price;
$query->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['between', 'price', $this ->price, $this -> to_price])
->andFilterWhere(['like', 'image', $this->image]);
$command = $query->createCommand();// Created $command works fine in mysql
return $dataProvider; //But in $dataProvider it doesnt return any result
}
这里怎么了?
答案 0 :(得分:0)
问题在空格$ this - > to_price和$ this - > price。 它应该是$ this-> to_price和$ this->价格。 因此,在过滤器行代码之间进行更改
->andFilterWhere(['between', 'price', $this ->price, $this -> to_price])
要
->andFilterWhere(['between', 'price', $this->price, $this->to_price])
$ this和 - >之后不允许使用空格运营商