我在yii2中使用gii来生成一个crud系统。 我需要编写这个sql来过滤表中的正确数据。
$date_now = date('Y-m-d');
$query = "SELECT * FROM reservation_db WHERE STR_TO_DATE(
日期,'%d-%m-%Y') > '$date_now'";
我知道这是错的,我将日期保存为数据库中的类型文本。因此,我需要使用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
。
* /
class ReservationSearch扩展ReservationDb
{
/ **
* @inheritdoc
* /
公共职能规则()
{
返回[
[['reservation_id','成人','孩子','infat','phone_num'],
'整数'],
[['name','date','time','session','seat_area','remark',
'payment_status','reservation_date'],'safe'],
]。
}
/**
* @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;
}
}
答案 0 :(得分:0)
试试这个:
public function search($params)
{
$date_now = date('Y-m-d');
$query = ReservationDb::find();
$query->andFilterCompare('STR_TO_DATE(date)', $date_now, '>');
.
.
.
}