Skip to content

Commit fa575f9

Browse files
authored
Merge pull request #2011 from iRedds/feature/entity-raw-data
Set the raw data array without any mutations for the Entity
2 parents b71a4c7 + 45c7501 commit fa575f9

4 files changed

Lines changed: 35 additions & 5 deletions

File tree

system/Database/MySQLi/Result.php

Lines changed: 6 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,14 @@ 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+
return empty($data = $this->fetchAssoc()) ? false : (new $className())->setAttributes($data);
167+
}
163168
return $this->resultID->fetch_object($className);
164169
}
165170

system/Database/Postgre/Result.php

Lines changed: 6 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,14 @@ 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+
return empty($data = $this->fetchAssoc()) ? false : (new $className())->setAttributes($data);
165+
}
161166
return pg_fetch_object($this->resultID, null, $className);
162167
}
163168

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->setAttributes($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 setAttributes(array $data)
437+
{
438+
$this->attributes = $data;
439+
$this->syncOriginal();
440+
return $this;
441+
}
442+
430443
//--------------------------------------------------------------------
431444

432445
/**

0 commit comments

Comments
 (0)