Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't break with Model::shouldBeStrict() enabled #18

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
composer.lock
.phpunit.result.cache
.php-cs-fixer.cache
.idea/
17 changes: 15 additions & 2 deletions src/VirtualColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ trait VirtualColumn
*/
public bool $dataEncoded = false;

public bool $dataNotLoaded = false;

protected function decodeVirtualColumn(): void
{
if (! $this->dataEncoded) {
if (! $this->dataEncoded || ! $this->dataNotLoaded) {
return;
}

Expand Down Expand Up @@ -65,6 +67,10 @@ protected function encodeAttributes(): void
return;
}

if ($this->dataNotLoaded) {
throw new \Exception('Data column was not loaded from the database. Make sure the data column is selected in the query.');
}

$dataColumn = static::getDataColumn();
$customColumns = static::getCustomColumns();
$attributes = array_filter($this->getAttributes(), fn ($key) => ! in_array($key, $customColumns), ARRAY_FILTER_USE_KEY);
Expand Down Expand Up @@ -107,7 +113,14 @@ protected function getAfterListeners(): array
return [
'retrieved' => [
function () {
// Always decode after model retrieval
// If the data column wasn't retrieved, don't decode it
if (($this->attributes[static::getDataColumn()] ?? null) === null) {
$this->dataNotLoaded = true;

return;
}

// Mark the data as encoded so that it doesn't get encoded again
$this->dataEncoded = true;

$this->decodeVirtualColumn();
Expand Down
28 changes: 28 additions & 0 deletions tests/VirtualColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,34 @@ public function models_extending_a_parent_model_using_virtualcolumn_get_encoded_
$this->assertSame($encodedBar->bar, 'bar');
}

/** @test */
public function model_doesnt_overwrite_when_selectively_fetching() {
$this->expectExceptionMessage('Data column was not loaded from the database. Make sure the data column is selected in the query.');

FooModel::create([
'id' => 1,
'foo' => 'bar'
]);

$foo = FooModel::query()->first(['id']);
$foo->bar = 'baz';
$foo->save();
}

/** @test */
public function decoding_works_with_strict_mode_enabled() {
FooModel::shouldBeStrict();

FooModel::create([
'id' => 1,
'foo' => 'bar'
]);

$id = FooModel::query()->pluck('id')->first();

$this->assertSame(1, $id);
}

// maybe add an explicit test that the saving() and updating() listeners don't run twice?

/** @test */
Expand Down
Loading