From 4b7165e5181326c72867abf33482d471eab6a94a Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 10 Aug 2023 16:58:29 +0900 Subject: [PATCH 1/5] docs: fix typos in comment --- system/Model.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Model.php b/system/Model.php index dc49f398c28d..e66f12092469 100644 --- a/system/Model.php +++ b/system/Model.php @@ -774,11 +774,11 @@ public function update($id = null, $data = null): bool } /** - * Takes a class an returns an array of it's public and protected + * Takes a class and returns an array of its public and protected * properties as an array with raw values. * * @param object|string $data - * @param bool $recursive If true, inner entities will be casted as array as well + * @param bool $recursive If true, inner entities will be cast as array as well * * @return array|null Array * From d5a85e3a81bf5fcdd74968a32c53397beff3fa27 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 10 Aug 2023 16:57:11 +0900 Subject: [PATCH 2/5] fix: Entity's primary key is cast when inserting --- system/Model.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/system/Model.php b/system/Model.php index e66f12092469..64e222150979 100644 --- a/system/Model.php +++ b/system/Model.php @@ -788,6 +788,21 @@ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recur { $properties = parent::objectToRawArray($data, $onlyChanged); + $primaryKey = null; + + // For Entity + if (method_exists($data, 'cast')) { + $cast = $data->cast(); + + // Disable Entity casting, because raw primary key data is needed for database. + $data->cast(false); + + $primaryKey = $data->{$this->primaryKey}; + + // Restore Entity cast setting. + $data->cast($cast); + } + // Always grab the primary key otherwise updates will fail. if ( method_exists($data, 'toRawArray') @@ -795,10 +810,10 @@ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recur ! empty($properties) && ! empty($this->primaryKey) && ! in_array($this->primaryKey, $properties, true) - && ! empty($data->{$this->primaryKey}) + && ! empty($primaryKey) ) ) { - $properties[$this->primaryKey] = $data->{$this->primaryKey}; + $properties[$this->primaryKey] = $primaryKey; } return $properties; From 366bcbe2d87deb26a0364f6a93de49b47dcf5ccf Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 14 Aug 2023 11:08:33 +0900 Subject: [PATCH 3/5] fix: use `instanceof Entity` `method_exists($data, 'cast')` is too loose, and not good. --- system/Model.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Model.php b/system/Model.php index 64e222150979..c0c8142fdad0 100644 --- a/system/Model.php +++ b/system/Model.php @@ -20,6 +20,7 @@ use CodeIgniter\Database\Exceptions\DatabaseException; use CodeIgniter\Database\Exceptions\DataException; use CodeIgniter\Database\Query; +use CodeIgniter\Entity\Entity; use CodeIgniter\Exceptions\ModelException; use CodeIgniter\I18n\Time; use CodeIgniter\Validation\ValidationInterface; @@ -790,8 +791,7 @@ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recur $primaryKey = null; - // For Entity - if (method_exists($data, 'cast')) { + if ($data instanceof Entity) { $cast = $data->cast(); // Disable Entity casting, because raw primary key data is needed for database. From d82a9bbdbaa36693122ae333bf204eaf1eec4284 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 14 Aug 2023 11:09:51 +0900 Subject: [PATCH 4/5] docs: add @TODO --- system/Model.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/system/Model.php b/system/Model.php index c0c8142fdad0..25b2b354b990 100644 --- a/system/Model.php +++ b/system/Model.php @@ -799,12 +799,13 @@ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recur $primaryKey = $data->{$this->primaryKey}; - // Restore Entity cast setting. + // Restore Entity casting setting. $data->cast($cast); } // Always grab the primary key otherwise updates will fail. if ( + // @TODO Should use `$data instanceof Entity`. method_exists($data, 'toRawArray') && ( ! empty($properties) From a8844650bafa9ad84228994bcd8da1f9f069505a Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 14 Aug 2023 11:18:22 +0900 Subject: [PATCH 5/5] chore: Model depends on Entity --- deptrac.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/deptrac.yaml b/deptrac.yaml index bf39d4f54734..6b1d0b9818e4 100644 --- a/deptrac.yaml +++ b/deptrac.yaml @@ -189,6 +189,7 @@ parameters: - I18n Model: - Database + - Entity - I18n - Pager - Validation