Skip to content

Commit

Permalink
Merge pull request #1930 from atishamte/model
Browse files Browse the repository at this point in the history
Model Changes w.r.t. #1773
  • Loading branch information
lonnieezell authored Apr 11, 2019
2 parents 81953eb + 93b5280 commit c51dea3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
15 changes: 4 additions & 11 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,14 +484,6 @@ public function set($key, string $value = '', bool $escape = null)
*/
public function save($data): bool
{
// If $data is using a custom class with public or protected
// properties representing the table elements, we need to grab
// them as an array.
if (is_object($data) && ! $data instanceof \stdClass)
{
$data = static::classToArray($data, $this->primaryKey, $this->dateFormat);
}

if (empty($data))
{
return true;
Expand Down Expand Up @@ -525,15 +517,16 @@ public function save($data): bool
* @param string|object $data
* @param string|null $primaryKey
* @param string $dateFormat
* @param boolean $onlyChanged
*
* @return array
* @throws \ReflectionException
*/
public static function classToArray($data, $primaryKey = null, string $dateFormat = 'datetime'): array
public static function classToArray($data, $primaryKey = null, string $dateFormat = 'datetime', bool $onlyChanged = true): array
{
if (method_exists($data, 'toRawArray'))
{
$properties = $data->toRawArray(true);
$properties = $data->toRawArray($onlyChanged);

// Always grab the primary key otherwise updates will fail.
if (! empty($properties) && ! empty($primaryKey) && ! in_array($primaryKey, $properties))
Expand Down Expand Up @@ -631,7 +624,7 @@ public function insert($data = null, bool $returnID = true)
// them as an array.
if (is_object($data) && ! $data instanceof \stdClass)
{
$data = static::classToArray($data, $this->primaryKey, $this->dateFormat);
$data = static::classToArray($data, $this->primaryKey, $this->dateFormat, false);
}

// If it's still a stdClass, go ahead and convert to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public function up()
'type' => 'VARCHAR',
'constraint' => 40,
],
'description' => ['type' => 'TEXT'],
'description' => [
'type' => 'TEXT',
'null' => true,
],
'created_at' => [
'type' => 'DATETIME',
'null' => true,
Expand Down
26 changes: 21 additions & 5 deletions tests/system/Database/Live/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ public function testUpdateBatchValidationFail()

public function testSelectAndEntitiesSaveOnlyChangedValues()
{
// Insert value in job table
$this->hasInDatabase('job', [
'name' => 'Rocket Scientist',
'description' => 'Plays guitar for Queen',
Expand All @@ -984,20 +985,35 @@ public function testSelectAndEntitiesSaveOnlyChangedValues()

$model = new EntityModel();

// get only id, name column
$job = $model->select('id, name')
->where('name', 'Rocket Scientist')
->first();
->where('name', 'Rocket Scientist')
->first();

// Hence getting Null as description column not in select clause
$this->assertNull($job->description);

// Equals with name to check, correct record fetched or not.
$this->assertEquals('Rocket Scientist', $job->name);

$job->description = 'Some guitar description';

// saving the result set with description as empty
$model->save($job);

// check for the record to same entry exists or not
$this->seeInDatabase('job', [
'id' => $job->id,
'name' => 'Rocket Scientist',
'description' => 'Plays guitar for Queen',
'id' => $job->id,
'name' => 'Rocket Scientist',
]);

// select all columns from job table
$job = $model->select('id, name, description')
->where('name', 'Rocket Scientist')
->first();

// check whether the Null value successfully updated or not
$this->assertEquals('Some guitar description', $job->description);
}

public function testUpdateNoPrimaryKey()
Expand Down

0 comments on commit c51dea3

Please sign in to comment.