基本上是ProductOffer表。 迁移
$this->createTable('product_offer', [
'id' => $this->primaryKey(),
'product_id' => $this->bigInteger(20) . ' NOT NULL',
'customer_id' => $this->bigInteger(20),
'coupon_code' => $this->string(),
'offer' => $this->string(),
'customer_name' => $this->string(),
'email_address' => $this->string(),
'phone_number' => $this->string(),
'note' => $this->text(),
'admin_note' => $this->text(),
'status' => $this->tinyInteger(),
'created' => $this->dateTime() . ' NOT NULL',
'modified' => $this->dateTime(),
]);
现在我在此表中有数据并创建了查询。
$stamp = mktime(0, 0, 0);
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address],
['>', 'created', date('m-d-Y H:i:s',$stamp)]
]);
echo "<pre>";
print_r($testQuery->all());
echo "</pre>";
结果是
Array
(
[0] => app\models\ProductOffer Object
(
[verifyCode] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 48
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07@gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => Rejected
[created] => 2018-05-16 04:38:28
[modified] =>
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 48
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07@gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => 0
[created] => 2018-05-16 04:38:28
[modified] =>
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_relationsDependencies:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
[1] => app\models\ProductOffer Object
(
[verifyCode] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 50
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07@gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => Rejected
[created] => 2018-05-17 04:39:55
[modified] =>
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 50
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07@gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => 0
[created] => 2018-05-17 04:39:55
[modified] =>
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_relationsDependencies:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
[2] => app\models\ProductOffer Object
(
[verifyCode] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 51
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07@gmail.com
[phone_number] => 958568574
[note] => test
[admin_note] =>
[status] => Rejected
[created] => 2018-05-17 04:43:29
[modified] =>
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 51
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07@gmail.com
[phone_number] => 958568574
[note] => test
[admin_note] =>
[status] => 0
[created] => 2018-05-17 04:43:29
[modified] =>
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_relationsDependencies:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
[3] => app\models\ProductOffer Object
(
[verifyCode] =>
[_attributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 55
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07@gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => Rejected
[created] => 2018-05-17 04:51:57
[modified] =>
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
(
[id] => 55
[product_id] => 111
[customer_id] =>
[coupon_code] =>
[offer] => 23
[customer_name] => Aljay Mallari
[email_address] => asrockg07@gmail.com
[phone_number] => 958568574
[note] => Test
[admin_note] =>
[status] => 0
[created] => 2018-05-17 04:51:57
[modified] =>
)
[_related:yii\db\BaseActiveRecord:private] => Array
(
)
[_relationsDependencies:yii\db\BaseActiveRecord:private] => Array
(
)
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array
(
)
[_eventWildcards:yii\base\Component:private] => Array
(
)
[_behaviors:yii\base\Component:private] => Array
(
)
)
);
今天在我们的时区是5/17/2018,我想获取今天创建的数据,但似乎我的查询不排除昨天创建的数据。
我已经尝试过表达这个
['<', 'created', new Expression('NOW()')]
和这个
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address],
['>', 'created', new Expression(date('m-d-Y H:i:s', $stamp))]
]);
echo "<pre>";
print_r($testQuery->all());
echo "</pre>";
现在我收到一个SQL错误
数据库异常 - yii \ db \ Exception SQLSTATE [42000]:语法错误或 访问冲突:1064您的SQL语法有错误;检查 手册,对应右边的MySQL服务器版本 在第1行'00:00:00'附近使用的语法正在执行的SQL是: SELECT * FROM
product_offer
WHERE(status
= 0)AND(product_id
= '111')AND(email_address
='asrockg07@gmail.com')AND(created
&gt; 05-17-2018 00:00:00)
答案 0 :(得分:2)
您可以将原始SQL内容与字符串格式的WHERE
调用一起使用,也可以将两个WHERE
调用一起使用。把它们放在一起,我们可以尝试以下方法:
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address]
])
->andWhere('created >= CURDATE()');
请注意,如果您想限制今天在任何时候创建的记录,我认为您希望在这里使用CURDATE
,这对应于今天午夜。
答案 1 :(得分:0)
您使用的日期格式不正确。传递印章应该date('Y-m-d H:i:s', $stamp)
或强迫午夜date('Y-m-d 00:00:00', $stamp)
。
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address],
['>', 'created', date('Y-m-d 00:00:00', $stamp)]
]);
此外,您以错误的方式使用yii\db\Expression
。您不能将值直接传递给Expression
,这可能会打开SQL注入。如果需要,你应该使用params。
$testQuery = self::find()->where([
'and',
['=', 'status', self::STATUS_REJECTED],
['=', 'product_id', $this->product_id],
['=', 'email_address', $this->email_address],
new Expression('created > :date', [':date' => date('Y-m-d 00:00:00', $stamp)]),
]);