我在yii2中使用gii生成了一个crud系统。我需要编写这个sql来过滤表中的正确数据。
$date_now = date('Y-m-d');
$query = "SELECT * FROM reservation_db WHERE STR_TO_DATE(date,'%d-%m-%Y') > '$date_now'";
我知道这是错误的,我将date类型文本保存在数据库中。因此,我需要使用STR_TO_DATE将字符串转换为日期,并使sql只选择大于今天的数据。
你知道我在哪里做这个sql吗?
这是我的ReservationSearch模型
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\ReservationDb;/** * ReservationSearch代表了关于app\models\ReservationDb的搜索表单背后的模型。*/类ReservationSearch扩展ReservationDb { /** * @inheritdoc */公共函数规则(){ return [[‘保留_id’,‘成人’,‘孩子’,'infat',‘电话号码’,‘整数’],[‘名称’,‘日期’,‘时间’,‘会话’,‘座位区域’,‘备注’,‘'payment_status','reservation_date',’安全‘],];}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$date_now = date('Y-m-d');
$query = ReservationDb::find();
$query->andFilterCompare( 'STR_TO_DATE(date)', '$date_now', '>');
$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;
}
// grid filtering conditions
$query->andFilterWhere([
'reservation_id' => $this->reservation_id,
'adult' => $this->adult,
'child' => $this->child,
'infat' => $this->infat,
'phone_num' => $this->phone_num,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'date', $this->date])
->andFilterWhere(['like', 'time', $this->time])
->andFilterWhere(['like', 'session', $this->session])
->andFilterWhere(['like', 'seat_area', $this->seat_area])
->andFilterWhere(['like', 'remark', $this->remark])
->andFilterWhere(['like', 'payment_status', $this->payment_status])
->andFilterWhere(['like', 'reservation_date', $this-
>reservation_date]);
return $dataProvider;
}}
发布于 2017-06-29 16:15:08
试试这个:
public function search($params)
{
$date_now = date('Y-m-d');
$query = ReservationDb::find();
$query->andFilterCompare('STR_TO_DATE(date)', $date_now, '>');
.
.
.
}https://stackoverflow.com/questions/44816248
复制相似问题