1

我有一个问题,我想知道这是否是在 yii2 中编写 BETWEEN 查询的正确方法。

在我声明的控制器代码中$to_price,这是我的 SearchModel 代码,但我总是得到与 for 相同的值null$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
}

如果不是,我没有得到任何结果,但我可以看到生成的命令在mysql

所以这里有什么问题???

4

1 回答 1

0

问题在于空间 $this ->to_price 和 $this ->price。它应该是 $this->to_price 和 $this->price。所以在过滤器行代码之间改变

->andFilterWhere(['between', 'price', $this ->price, $this -> to_price])

->andFilterWhere(['between', 'price', $this->price, $this->to_price])

$this 和 -> 运算符后不允许有空格

于 2017-08-20T07:17:10.993 回答