diff --git a/system/Database/MySQLi/Result.php b/system/Database/MySQLi/Result.php index 4299b369ba2d..630a4f7f3788 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,14 @@ 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)) + { + 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 6842e36ec017..106a7e18298c 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,14 @@ 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)) + { + 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 014a11fc7417..3d567152d210 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->setAttributes($row); + } + $classSet = \Closure::bind(function ($key, $value) { $this->$key = $value; }, $classObj, $className diff --git a/system/Entity.php b/system/Entity.php index 1e6599010de8..3c8b19a7f994 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 setAttributes(array $data) + { + $this->attributes = $data; + $this->syncOriginal(); + return $this; + } + //-------------------------------------------------------------------- /**