Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the raw data array without any mutations for the Entity #2011

Merged
merged 2 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion system/Database/MySQLi/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\ResultInterface;
use CodeIgniter\Entity;

/**
* Result for MySQLi
Expand Down Expand Up @@ -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);
}

Expand Down
7 changes: 6 additions & 1 deletion system/Database/Postgre/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\ResultInterface;
use CodeIgniter\Entity;

/**
* Result for Postgre
Expand Down Expand Up @@ -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);
}

Expand Down
7 changes: 7 additions & 0 deletions system/Database/SQLite3/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\ResultInterface;
use CodeIgniter\Entity;

/**
* Result for SQLite3
Expand Down Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions system/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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))
{
Expand Down Expand Up @@ -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;
}

//--------------------------------------------------------------------

/**
Expand Down