Skip to content

Commit

Permalink
Allow stdObject to be saved in Models. Fixes #329
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnieezell committed Dec 19, 2016
1 parent 0e9204f commit 3610f1f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 17 additions & 1 deletion system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Model
*
* @var array
*/
protected $allowedFields = [];
protected $allowedFields = ['name'];

/**
* If true, will set created_at, and updated_at
Expand Down Expand Up @@ -630,6 +630,14 @@ public function insert($data)
$data = $this->classToArray($data);
}

// If it's still a stdClass, go ahead and convert to
// an array so doProtectFields and other model methods
// don't have to do special checks.
if (is_object($data))
{
$data = (array)$data;
}

// Validate data before saving.
if ($this->skipValidation === false)
{
Expand Down Expand Up @@ -684,6 +692,14 @@ public function update($id, $data)
$data = $this->classToArray($data);
}

// If it's still a stdClass, go ahead and convert to
// an array so doProtectFields and other model methods
// don't have to do special checks.
if (is_object($data))
{
$data = (array)$data;
}

// Validate data before saving.
if ($this->skipValidation === false)
{
Expand Down
2 changes: 0 additions & 2 deletions tests/system/Database/Live/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,6 @@ public function testSaveProtected()
$data->name = 'Engineer';
$data->description = 'A fancier term for Developer.';

$this->setExpectedException('CodeIgniter\DatabaseException');

$model->protect(true)->save($data);
}

Expand Down

0 comments on commit 3610f1f

Please sign in to comment.