Skip to content

Commit

Permalink
Merge pull request #7 from stein-j/master
Browse files Browse the repository at this point in the history
Sync with original attributes
  • Loading branch information
stancl authored Jan 4, 2022
2 parents 41d300b + 9b31176 commit c5e70e8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/VirtualColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protected static function decodeVirtualColumn(self $model): void

foreach ($model->getAttribute(static::getDataColumn()) ?? [] as $key => $value) {
$model->setAttribute($key, $value);
$model->syncOriginalAttribute($key);
}

$model->setAttribute(static::getDataColumn(), null);
Expand All @@ -52,6 +53,7 @@ protected static function encodeAttributes(self $model): void
]));

unset($model->attributes[$key]);
unset($model->original[$key]);
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/VirtualColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function keys_which_dont_have_their_own_column_go_into_data_json_column()
// Model has the correct structure when retrieved
$model = MyModel::first();
$this->assertSame('bar', $model->foo);
$this->assertSame('bar', $model->getOriginal('foo'));
$this->assertSame(null, $model->data);

// Model can be updated
Expand All @@ -43,7 +44,9 @@ public function keys_which_dont_have_their_own_column_go_into_data_json_column()
]);

$this->assertSame('baz', $model->foo);
$this->assertSame('baz', $model->getOriginal('foo'));
$this->assertSame('xyz', $model->abc);
$this->assertSame('xyz', $model->getOriginal('abc'));
$this->assertSame(null, $model->data);

// Model can be retrieved after update & is structure correctly
Expand Down

0 comments on commit c5e70e8

Please sign in to comment.