Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Entity::toRawArray() may return Time object #8302

Open
kenjis opened this issue Dec 7, 2023 · 4 comments · May be fixed by #9286
Open

Bug: Entity::toRawArray() may return Time object #8302

kenjis opened this issue Dec 7, 2023 · 4 comments · May be fixed by #9286
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@kenjis
Copy link
Member

kenjis commented Dec 7, 2023

PHP Version

8.1

CodeIgniter4 Version

4.4.3 and develop

CodeIgniter4 Installation Method

Git

Which operating systems have you tested for this bug?

macOS

Which server did you use?

cli-server (PHP built-in webserver)

Database

MySQL 8.0.34

What happened?

The following code returns Time object.

        $user->updated_at = '2023-12-12 12:12:12';
        var_dump($user->toRawArray());

Steps to Reproduce

+----+------------+--------------------+---------+--------------------+--------------------+------------+
| id | name       | email              | country | created_at         | updated_at         | deleted_at |
+----+------------+--------------------+---------+--------------------+--------------------+------------+
| 1  | John Smith | [email protected]... | US      | 2023-12-07 04:0... | 2023-12-07 04:0... |            |
+----+------------+--------------------+---------+--------------------+--------------------+------------+
<?php

namespace App\Models;

use App\Entities\User;
use CodeIgniter\Model;

class UserModel extends Model
{
    protected $table            = 'user';
    protected $returnType       = User::class;
    protected $allowedFields    = [
        'name',
        'email',
        'country',
    ];

    // Dates
    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';
}
<?php

namespace App\Entities;

use CodeIgniter\Entity\Entity;

class User extends Entity
{
    protected $datamap = [];
    protected $dates   = ['created_at', 'updated_at', 'deleted_at'];
    protected $casts   = [];
}
    public function index()
    {
        $users = new UserModel();
        /** @var User $user */
        $user = $users->find(1);
        var_dump($user->toRawArray());

        $user->updated_at = '2023-12-12 12:12:12';
        var_dump($user->toRawArray());

        $user = $users->find(1);
        var_dump($user->toRawArray());
    }
/Users/kenji/work/codeigniter/official/CodeIgniter4/app/Controllers/Home.php:15:
array (size=7)
  'id' => string '1' (length=1)
  'name' => string 'John Smith' (length=10)
  'email' => string '[email protected]' (length=16)
  'country' => string 'US' (length=2)
  'created_at' => string '2023-12-07 04:00:48' (length=19)
  'updated_at' => string '2023-12-07 04:00:48' (length=19)
  'deleted_at' => null

/Users/kenji/work/codeigniter/official/CodeIgniter4/app/Controllers/Home.php:18:
array (size=7)
  'id' => string '1' (length=1)
  'name' => string 'John Smith' (length=10)
  'email' => string '[email protected]' (length=16)
  'country' => string 'US' (length=2)
  'created_at' => string '2023-12-07 04:00:48' (length=19)
  'updated_at' => 
    object(CodeIgniter\I18n\Time)[88]
      protected 'timezone' => 
        object(DateTimeZone)[90]
          public 'timezone_type' => int 3
          public 'timezone' => string 'UTC' (length=3)
      protected 'locale' => string 'en' (length=2)
      protected 'toStringFormat' => string 'yyyy-MM-dd HH:mm:ss' (length=19)
      public 'date' => string '2023-12-12 12:12:12.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'UTC' (length=3)
  'deleted_at' => null

/Users/kenji/work/codeigniter/official/CodeIgniter4/app/Controllers/Home.php:21:
array (size=7)
  'id' => string '1' (length=1)
  'name' => string 'John Smith' (length=10)
  'email' => string '[email protected]' (length=16)
  'country' => string 'US' (length=2)
  'created_at' => string '2023-12-07 04:00:48' (length=19)
  'updated_at' => string '2023-12-07 04:00:48' (length=19)
  'deleted_at' => null

Expected Output

Returns string value '2023-12-12 12:12:12'?

Anything else?

No response

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Dec 7, 2023
@neznaika0
Copy link
Contributor

After calling find(), nothing changes, because attributes are just copied as strings. After the change, castAs() is applied.

@kenjis
Copy link
Member Author

kenjis commented Dec 8, 2023

Yes, if we change the date and save the object, toRawArray() returns the Time object.

@neznaika0
Copy link
Contributor

@kenjis got a question.
What should $original contain? Data from the table as primitive types or modified data after castAs()? This is misleading. If we select an object from the table, apply casting and $entity->syncOriginal(), nothing will change globally (the data is relatively old - date type changed to Time), but the entity will change $original

@kenjis
Copy link
Member Author

kenjis commented Dec 13, 2023

The $original is only for hasChanged().
So it should contain the data for it.

The current behavior:

    public function index()
    {
        $users = new UserModel();

        /** @var User $user */
        $user = $users->find(1);
        $user->updated_at = '2023-12-12 12:12:12';
        var_dump($user);
    }
/Users/kenji/work/codeigniter/official/CodeIgniter4/app/Controllers/Home.php:17:
object(App\Entities\User)[89]
  protected 'datamap' => 
    array (size=0)
      empty
  protected 'dates' => 
    array (size=3)
      0 => string 'created_at' (length=10)
      1 => string 'updated_at' (length=10)
      2 => string 'deleted_at' (length=10)
  protected 'casts' => 
    array (size=0)
      empty
  protected 'castHandlers' => 
    array (size=0)
      empty
  private array 'defaultCastHandlers' (CodeIgniter\Entity\Entity) => 
    array (size=15)
      'array' => string 'CodeIgniter\Entity\Cast\ArrayCast' (length=33)
      'bool' => string 'CodeIgniter\Entity\Cast\BooleanCast' (length=35)
      'boolean' => string 'CodeIgniter\Entity\Cast\BooleanCast' (length=35)
      'csv' => string 'CodeIgniter\Entity\Cast\CSVCast' (length=31)
      'datetime' => string 'CodeIgniter\Entity\Cast\DatetimeCast' (length=36)
      'double' => string 'CodeIgniter\Entity\Cast\FloatCast' (length=33)
      'float' => string 'CodeIgniter\Entity\Cast\FloatCast' (length=33)
      'int' => string 'CodeIgniter\Entity\Cast\IntegerCast' (length=35)
      'integer' => string 'CodeIgniter\Entity\Cast\IntegerCast' (length=35)
      'int-bool' => string 'CodeIgniter\Entity\Cast\IntBoolCast' (length=35)
      'json' => string 'CodeIgniter\Entity\Cast\JsonCast' (length=32)
      'object' => string 'CodeIgniter\Entity\Cast\ObjectCast' (length=34)
      'string' => string 'CodeIgniter\Entity\Cast\StringCast' (length=34)
      'timestamp' => string 'CodeIgniter\Entity\Cast\TimestampCast' (length=37)
      'uri' => string 'CodeIgniter\Entity\Cast\URICast' (length=31)
  protected 'attributes' => 
    array (size=7)
      'id' => string '1' (length=1)
      'name' => string 'John Smith' (length=10)
      'email' => string '[email protected]' (length=16)
      'country' => string 'US' (length=2)
      'created_at' => string '2023-12-13 09:58:48' (length=19)
      'updated_at' => 
        object(CodeIgniter\I18n\Time)[88]
          protected 'timezone' => 
            object(DateTimeZone)[90]
              ...
          protected 'locale' => string 'en' (length=2)
          protected 'toStringFormat' => string 'yyyy-MM-dd HH:mm:ss' (length=19)
          public 'date' => string '2023-12-12 12:12:12.000000' (length=26)
          public 'timezone_type' => int 3
          public 'timezone' => string 'UTC' (length=3)
      'deleted_at' => null
  protected 'original' => 
    array (size=7)
      'id' => string '1' (length=1)
      'name' => string 'John Smith' (length=10)
      'email' => string '[email protected]' (length=16)
      'country' => string 'US' (length=2)
      'created_at' => string '2023-12-13 09:58:48' (length=19)
      'updated_at' => string '2023-12-13 09:58:48' (length=19)
      'deleted_at' => null
  private bool '_cast' (CodeIgniter\Entity\Entity) => boolean true

@ping-yee ping-yee linked a pull request Nov 18, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants