From 2ecff6437760cdcbfccbf9777478c60df330f4b5 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 29 Nov 2023 17:47:04 +0900 Subject: [PATCH] fix: update does not work when using custom entity with toRawArray() --- system/BaseModel.php | 1 + system/Model.php | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/system/BaseModel.php b/system/BaseModel.php index 1d854256a63c..5f436f565e55 100644 --- a/system/BaseModel.php +++ b/system/BaseModel.php @@ -1698,6 +1698,7 @@ protected function objectToArray($data, bool $onlyChanged = true, bool $recursiv */ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recursive = false): ?array { + // @TODO Should add an interface for this? if (method_exists($data, 'toRawArray')) { $properties = $data->toRawArray($onlyChanged, $recursive); } else { diff --git a/system/Model.php b/system/Model.php index 3a055531cb5b..8a1ed0bb324b 100644 --- a/system/Model.php +++ b/system/Model.php @@ -798,7 +798,11 @@ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recur { $properties = parent::objectToRawArray($data, $onlyChanged); - $primaryKey = null; + if ($onlyChanged === false) { + return $properties; + } + + $primaryKey = $data->{$this->primaryKey} ?? null; if ($data instanceof Entity) { $cast = $data->cast(); @@ -814,12 +818,11 @@ protected function objectToRawArray($data, bool $onlyChanged = true, bool $recur // Always grab the primary key otherwise updates will fail. if ( - // @TODO Should use `$data instanceof Entity`. + // @TODO Should add an interface for this? method_exists($data, 'toRawArray') && ( ! empty($properties) && ! empty($this->primaryKey) - && ! in_array($this->primaryKey, $properties, true) && ! empty($primaryKey) ) ) {