Repository navigation
Bug: Entity::toRawArray() may return Time object #8302
Copy link
Copy link
Open
Labels
bugVerified issues on the current code behavior or pull requests that will fix themVerified issues on the current code behavior or pull requests that will fix them
Description
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 | john@example.co... | 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 'john@example.com' (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 'john@example.com' (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 'john@example.com' (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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugVerified issues on the current code behavior or pull requests that will fix themVerified issues on the current code behavior or pull requests that will fix them
Activity
neznaika0 commentedon Dec 8, 2023
After calling
find(), nothing changes, because attributes are just copied as strings. After the change,castAs()is applied.kenjis commentedon Dec 8, 2023
Yes, if we change the date and save the object,
toRawArray()returns theTimeobject.neznaika0 commentedon Dec 13, 2023
@kenjis got a question.
What should
$originalcontain? Data from the table as primitive types or modified data aftercastAs()? 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 toTime), but the entity will change$originalkenjis commentedon Dec 13, 2023
The
$originalis only forhasChanged().So it should contain the data for it.
The current behavior:
fix: toRawArray() should convert DateTime objects to strings
fix: toRawArray() converts DateTime objects to strings
fix: toRawArray() converts DateTime objects to strings
fix: toRawArray() converts DateTime objects to strings
fix: toRawArray() converts DateTime objects to strings
fix: Entity::toRawArray() should convert DateTime objects to strings
fix: only convert DateTimeInterface in toRawArray for non-recursive c…
fix: Entity::toRawArray() converts DateTime objects to strings for no…
fix: toRawArray() converts DateTime objects to strings