PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 8, 2022

[FIXED] Yii 2 how to get all data within current day

 February 08, 2022     activerecord, mysql, php, yii, yii2     No comments   

Issue

Basically ProductOffer table. Migration

$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(),
]);

Now I have data in this table and created query.

$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>";

The result is

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
            (
            )

    )
);

Today in our time zone is 5/17/2018, I want to get data that created today from but it seems my query is not excluding the data that created yesterday.

I already try expression this

['<', 'created', new Expression('NOW()')]

and this

$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>";

And now I'm getting an SQL error

Database Exception – yii\db\Exception SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '00:00:00)' at line 1 The SQL being executed was: SELECT * FROM product_offer WHERE (status = 0) AND (product_id = '111') AND (email_address = 'asrockg07@gmail.com') AND (created > 05-17-2018 00:00:00)


Solution

You may use raw SQL content with a WHERE call in string format, and it is also possible to and together two WHERE calls. Putting this together, we can try the following:

$testQuery = self::find()->where([
    'and',
    ['=', 'status', self::STATUS_REJECTED],
    ['=', 'product_id', $this->product_id],
    ['=', 'email_address', $this->email_address]
])
->andWhere('created >= CURDATE()');

Note that I think you want to use CURDATE here, which corresponds to today at midnight, if you want to restrict to records created at any point today.



Answered By - Tim Biegeleisen
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing