yii2如何获取当天的所有数据

o8x7eapl  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(433)

基本上是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
            (
            )

    )
);

今天的时区是2018年5月17日,我想从中获取今天创建的数据,但我的查询似乎不排除昨天创建的数据。
我已经试过了

['<', '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 在哪里( status =0)和( product_id =“111”)和( email_address = 'asrockg07@gmail.com')和( created > 05-17-2018 00:00:00)

bjp0bcyl

bjp0bcyl1#

可以将原始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 这里,对应于今天午夜,如果您想限制到今天任何时候创建的记录。

5lhxktic

5lhxktic2#

你的日期格式不正确。应该是的 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)]),
]);

相关问题