From 8a9b67b9d2e6c8048ef7443cfc3a2735814e8dcc Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Fri, 17 May 2019 02:49:26 +0800 Subject: [PATCH 1/2] Set the raw data array without any mutations for the Entity Signed-off-by: Andrey Pyzhikov <5071@mail.ru> --- system/Database/MySQLi/Result.php | 8 +++++++- system/Database/Postgre/Result.php | 8 +++++++- system/Database/SQLite3/Result.php | 7 +++++++ system/Entity.php | 19 ++++++++++++++++--- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/system/Database/MySQLi/Result.php b/system/Database/MySQLi/Result.php index 4299b369ba2d..eb11196120db 100644 --- a/system/Database/MySQLi/Result.php +++ b/system/Database/MySQLi/Result.php @@ -40,6 +40,7 @@ use CodeIgniter\Database\BaseResult; use CodeIgniter\Database\ResultInterface; +use CodeIgniter\Entity; /** * Result for MySQLi @@ -156,10 +157,15 @@ protected function fetchAssoc() * * @param string $className * - * @return object + * @return object|boolean|Entity */ protected function fetchObject(string $className = 'stdClass') { + if (is_subclass_of($className, Entity::class)) + { + $data = $this->fetchAssoc(); + return empty($data) ? false : (new $className())->setRawArray($data); + } return $this->resultID->fetch_object($className); } diff --git a/system/Database/Postgre/Result.php b/system/Database/Postgre/Result.php index 6842e36ec017..c3654545c749 100644 --- a/system/Database/Postgre/Result.php +++ b/system/Database/Postgre/Result.php @@ -40,6 +40,7 @@ use CodeIgniter\Database\BaseResult; use CodeIgniter\Database\ResultInterface; +use CodeIgniter\Entity; /** * Result for Postgre @@ -154,10 +155,15 @@ protected function fetchAssoc() * * @param string $className * - * @return object + * @return object|boolean|Entity */ protected function fetchObject(string $className = 'stdClass') { + if (is_subclass_of($className, Entity::class)) + { + $data = $this->fetchAssoc(); + return empty($data) ? false : (new $className())->setRawArray($data); + } return pg_fetch_object($this->resultID, null, $className); } diff --git a/system/Database/SQLite3/Result.php b/system/Database/SQLite3/Result.php index 014a11fc7417..7c3f6b0981da 100644 --- a/system/Database/SQLite3/Result.php +++ b/system/Database/SQLite3/Result.php @@ -40,6 +40,7 @@ use CodeIgniter\Database\BaseResult; use CodeIgniter\Database\Exceptions\DatabaseException; use CodeIgniter\Database\ResultInterface; +use CodeIgniter\Entity; /** * Result for SQLite3 @@ -182,6 +183,12 @@ protected function fetchObject(string $className = 'stdClass') } $classObj = new $className(); + + if (is_subclass_of($className, Entity::class)) + { + return $classObj->setRawArray($row); + } + $classSet = \Closure::bind(function ($key, $value) { $this->$key = $value; }, $classObj, $className diff --git a/system/Entity.php b/system/Entity.php index 1e6599010de8..78970f6623e4 100644 --- a/system/Entity.php +++ b/system/Entity.php @@ -238,7 +238,7 @@ public function syncOriginal() * Checks a property to see if it has changed since the entity was created. * Or, without a parameter, checks if any properties have changed. * - * @param ?string $key + * @param string $key * * @return boolean */ @@ -247,9 +247,9 @@ public function hasChanged(string $key = null): bool // If no parameter was given then check all attributes if ($key === null) { - return $this->original !== $this->attributes; + return $this->original !== $this->attributes; } - + // Key doesn't exist in either if (! array_key_exists($key, $this->original) && ! array_key_exists($key, $this->attributes)) { @@ -427,6 +427,19 @@ public function __isset(string $key): bool return isset($this->attributes[$key]); } + /** + * Set raw data array without any mutations + * + * @param array $data + * @return $this + */ + public function setRawArray(array $data) + { + $this->attributes = $data; + $this->syncOriginal(); + return $this; + } + //-------------------------------------------------------------------- /** From 45c7501e8f26c47ea9cfb6e1f11ec297f2d75dee Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Sat, 18 May 2019 13:39:28 +0800 Subject: [PATCH 2/2] Rename method Entity::setRawArray() to Entity::setAttributes() Signed-off-by: Andrey Pyzhikov <5071@mail.ru> --- system/Database/MySQLi/Result.php | 3 +-- system/Database/Postgre/Result.php | 3 +-- system/Database/SQLite3/Result.php | 2 +- system/Entity.php | 2 +- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/system/Database/MySQLi/Result.php b/system/Database/MySQLi/Result.php index eb11196120db..630a4f7f3788 100644 --- a/system/Database/MySQLi/Result.php +++ b/system/Database/MySQLi/Result.php @@ -163,8 +163,7 @@ protected function fetchObject(string $className = 'stdClass') { if (is_subclass_of($className, Entity::class)) { - $data = $this->fetchAssoc(); - return empty($data) ? false : (new $className())->setRawArray($data); + return empty($data = $this->fetchAssoc()) ? false : (new $className())->setAttributes($data); } return $this->resultID->fetch_object($className); } diff --git a/system/Database/Postgre/Result.php b/system/Database/Postgre/Result.php index c3654545c749..106a7e18298c 100644 --- a/system/Database/Postgre/Result.php +++ b/system/Database/Postgre/Result.php @@ -161,8 +161,7 @@ protected function fetchObject(string $className = 'stdClass') { if (is_subclass_of($className, Entity::class)) { - $data = $this->fetchAssoc(); - return empty($data) ? false : (new $className())->setRawArray($data); + return empty($data = $this->fetchAssoc()) ? false : (new $className())->setAttributes($data); } return pg_fetch_object($this->resultID, null, $className); } diff --git a/system/Database/SQLite3/Result.php b/system/Database/SQLite3/Result.php index 7c3f6b0981da..3d567152d210 100644 --- a/system/Database/SQLite3/Result.php +++ b/system/Database/SQLite3/Result.php @@ -186,7 +186,7 @@ protected function fetchObject(string $className = 'stdClass') if (is_subclass_of($className, Entity::class)) { - return $classObj->setRawArray($row); + return $classObj->setAttributes($row); } $classSet = \Closure::bind(function ($key, $value) { diff --git a/system/Entity.php b/system/Entity.php index 78970f6623e4..3c8b19a7f994 100644 --- a/system/Entity.php +++ b/system/Entity.php @@ -433,7 +433,7 @@ public function __isset(string $key): bool * @param array $data * @return $this */ - public function setRawArray(array $data) + public function setAttributes(array $data) { $this->attributes = $data; $this->syncOriginal();