Skip to content

Commit 8a9b67b

Browse files
committed
Set the raw data array without any mutations for the Entity
Signed-off-by: Andrey Pyzhikov <5071@mail.ru>
1 parent c8c9091 commit 8a9b67b

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

system/Database/MySQLi/Result.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
use CodeIgniter\Database\BaseResult;
4242
use CodeIgniter\Database\ResultInterface;
43+
use CodeIgniter\Entity;
4344

4445
/**
4546
* Result for MySQLi
@@ -156,10 +157,15 @@ protected function fetchAssoc()
156157
*
157158
* @param string $className
158159
*
159-
* @return object
160+
* @return object|boolean|Entity
160161
*/
161162
protected function fetchObject(string $className = 'stdClass')
162163
{
164+
if (is_subclass_of($className, Entity::class))
165+
{
166+
$data = $this->fetchAssoc();
167+
return empty($data) ? false : (new $className())->setRawArray($data);
168+
}
163169
return $this->resultID->fetch_object($className);
164170
}
165171

system/Database/Postgre/Result.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
use CodeIgniter\Database\BaseResult;
4242
use CodeIgniter\Database\ResultInterface;
43+
use CodeIgniter\Entity;
4344

4445
/**
4546
* Result for Postgre
@@ -154,10 +155,15 @@ protected function fetchAssoc()
154155
*
155156
* @param string $className
156157
*
157-
* @return object
158+
* @return object|boolean|Entity
158159
*/
159160
protected function fetchObject(string $className = 'stdClass')
160161
{
162+
if (is_subclass_of($className, Entity::class))
163+
{
164+
$data = $this->fetchAssoc();
165+
return empty($data) ? false : (new $className())->setRawArray($data);
166+
}
161167
return pg_fetch_object($this->resultID, null, $className);
162168
}
163169

system/Database/SQLite3/Result.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
use CodeIgniter\Database\BaseResult;
4141
use CodeIgniter\Database\Exceptions\DatabaseException;
4242
use CodeIgniter\Database\ResultInterface;
43+
use CodeIgniter\Entity;
4344

4445
/**
4546
* Result for SQLite3
@@ -182,6 +183,12 @@ protected function fetchObject(string $className = 'stdClass')
182183
}
183184

184185
$classObj = new $className();
186+
187+
if (is_subclass_of($className, Entity::class))
188+
{
189+
return $classObj->setRawArray($row);
190+
}
191+
185192
$classSet = \Closure::bind(function ($key, $value) {
186193
$this->$key = $value;
187194
}, $classObj, $className

system/Entity.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public function syncOriginal()
238238
* Checks a property to see if it has changed since the entity was created.
239239
* Or, without a parameter, checks if any properties have changed.
240240
*
241-
* @param ?string $key
241+
* @param string $key
242242
*
243243
* @return boolean
244244
*/
@@ -247,9 +247,9 @@ public function hasChanged(string $key = null): bool
247247
// If no parameter was given then check all attributes
248248
if ($key === null)
249249
{
250-
return $this->original !== $this->attributes;
250+
return $this->original !== $this->attributes;
251251
}
252-
252+
253253
// Key doesn't exist in either
254254
if (! array_key_exists($key, $this->original) && ! array_key_exists($key, $this->attributes))
255255
{
@@ -427,6 +427,19 @@ public function __isset(string $key): bool
427427
return isset($this->attributes[$key]);
428428
}
429429

430+
/**
431+
* Set raw data array without any mutations
432+
*
433+
* @param array $data
434+
* @return $this
435+
*/
436+
public function setRawArray(array $data)
437+
{
438+
$this->attributes = $data;
439+
$this->syncOriginal();
440+
return $this;
441+
}
442+
430443
//--------------------------------------------------------------------
431444

432445
/**

0 commit comments

Comments
 (0)