Skip to content

Commit

Permalink
Merge pull request #1373 from nowackipawel/patch-15
Browse files Browse the repository at this point in the history
Turning off casting for db insert/save
  • Loading branch information
lonnieezell authored Oct 31, 2018
2 parents 7237d17 + 91035ad commit 7f2e6a5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions system/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ class Entity
*/
protected $_original = [];

/**
* Holds info whenever prperties have to be casted
*
* @var boolean
**/
private $_cast = true;

/**
* Allows filling in Entity parameters during construction.
*
Expand Down Expand Up @@ -133,11 +140,13 @@ public function fill(array $data)
* applied to them.
*
* @param bool $onlyChanged If true, only return values that have changed since object creation
* @param bool $cast If true, properties will be casted.
*
* @return array
*/
public function toArray(bool $onlyChanged = false): array
public function toArray(bool $onlyChanged = false, bool $cast = true): array
{
$this->_cast = $cast;
$return = [];

// we need to loop over our properties so that we
Expand All @@ -164,7 +173,7 @@ public function toArray(bool $onlyChanged = false): array
$return[$from] = $this->__get($to);
}
}

$this->_cast = true;
return $return;
}

Expand Down Expand Up @@ -211,7 +220,7 @@ public function __get(string $key)
$result = $this->mutateDate($result);
}
// Or cast it as something?
else if (isset($this->_options['casts'][$key]) && ! empty($this->_options['casts'][$key]))
else if ($this->_cast && isset($this->_options['casts'][$key]) && ! empty($this->_options['casts'][$key]))
{
$result = $this->castAs($result, $this->_options['casts'][$key]);
}
Expand Down
2 changes: 1 addition & 1 deletion system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public static function classToArray($data, string $dateFormat = 'datetime'): arr
{
if (method_exists($data, 'toArray'))
{
$properties = $data->toArray(true);
$properties = $data->toArray(true, false);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion tests/system/View/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function testParseLoopEntityProperties()
$power = new class extends \CodeIgniter\Entity {
public $foo = 'bar';
protected $bar = 'baz';
public function toArray(bool $onlyChanged = false): array
public function toArray(bool $onlyChanged = false, bool $cast = true): array
{
return [
'foo' => $this->foo,
Expand Down

0 comments on commit 7f2e6a5

Please sign in to comment.