Skip to content

Commit c51dea3

Browse files
authored
Merge pull request #1930 from atishamte/model
Model Changes w.r.t. #1773
2 parents 81953eb + 93b5280 commit c51dea3

3 files changed

Lines changed: 29 additions & 17 deletions

File tree

system/Model.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -484,14 +484,6 @@ public function set($key, string $value = '', bool $escape = null)
484484
*/
485485
public function save($data): bool
486486
{
487-
// If $data is using a custom class with public or protected
488-
// properties representing the table elements, we need to grab
489-
// them as an array.
490-
if (is_object($data) && ! $data instanceof \stdClass)
491-
{
492-
$data = static::classToArray($data, $this->primaryKey, $this->dateFormat);
493-
}
494-
495487
if (empty($data))
496488
{
497489
return true;
@@ -525,15 +517,16 @@ public function save($data): bool
525517
* @param string|object $data
526518
* @param string|null $primaryKey
527519
* @param string $dateFormat
520+
* @param boolean $onlyChanged
528521
*
529522
* @return array
530523
* @throws \ReflectionException
531524
*/
532-
public static function classToArray($data, $primaryKey = null, string $dateFormat = 'datetime'): array
525+
public static function classToArray($data, $primaryKey = null, string $dateFormat = 'datetime', bool $onlyChanged = true): array
533526
{
534527
if (method_exists($data, 'toRawArray'))
535528
{
536-
$properties = $data->toRawArray(true);
529+
$properties = $data->toRawArray($onlyChanged);
537530

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

637630
// If it's still a stdClass, go ahead and convert to

tests/_support/Database/Migrations/20160428212500_Create_test_tables.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public function up()
4646
'type' => 'VARCHAR',
4747
'constraint' => 40,
4848
],
49-
'description' => ['type' => 'TEXT'],
49+
'description' => [
50+
'type' => 'TEXT',
51+
'null' => true,
52+
],
5053
'created_at' => [
5154
'type' => 'DATETIME',
5255
'null' => true,

tests/system/Database/Live/ModelTest.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,7 @@ public function testUpdateBatchValidationFail()
976976

977977
public function testSelectAndEntitiesSaveOnlyChangedValues()
978978
{
979+
// Insert value in job table
979980
$this->hasInDatabase('job', [
980981
'name' => 'Rocket Scientist',
981982
'description' => 'Plays guitar for Queen',
@@ -984,20 +985,35 @@ public function testSelectAndEntitiesSaveOnlyChangedValues()
984985

985986
$model = new EntityModel();
986987

988+
// get only id, name column
987989
$job = $model->select('id, name')
988-
->where('name', 'Rocket Scientist')
989-
->first();
990+
->where('name', 'Rocket Scientist')
991+
->first();
990992

993+
// Hence getting Null as description column not in select clause
991994
$this->assertNull($job->description);
995+
996+
// Equals with name to check, correct record fetched or not.
992997
$this->assertEquals('Rocket Scientist', $job->name);
993998

999+
$job->description = 'Some guitar description';
1000+
1001+
// saving the result set with description as empty
9941002
$model->save($job);
9951003

1004+
// check for the record to same entry exists or not
9961005
$this->seeInDatabase('job', [
997-
'id' => $job->id,
998-
'name' => 'Rocket Scientist',
999-
'description' => 'Plays guitar for Queen',
1006+
'id' => $job->id,
1007+
'name' => 'Rocket Scientist',
10001008
]);
1009+
1010+
// select all columns from job table
1011+
$job = $model->select('id, name, description')
1012+
->where('name', 'Rocket Scientist')
1013+
->first();
1014+
1015+
// check whether the Null value successfully updated or not
1016+
$this->assertEquals('Some guitar description', $job->description);
10011017
}
10021018

10031019
public function testUpdateNoPrimaryKey()

0 commit comments

Comments
 (0)