Skip to content

Commit

Permalink
fix(Model): fromArray 包含虚拟字段触发 Invalid virtual column: 错误
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Nov 22, 2022
1 parent ad41224 commit f38dc31
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ protected function fromArray(iterable $array): self
}

if ($this->hasVirtual($name)) {
$this->setVirtualValue($name, $value);
$this->callSetter($name, $value);
}
}
return $this;
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/Model/Fixture/TestVirtual.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TestVirtual extends BaseModel
protected $virtual = [
'virtual_column',
'full_name',
'only_get',
];

protected $virtualColumnValue;
Expand Down Expand Up @@ -51,4 +52,9 @@ public function setFullNameAttribute($fullName)
{
[$this->first_name, $this->last_name] = explode(' ', $fullName);
}

public function getOnlyGetAttribute()
{
return 'test';
}
}
11 changes: 11 additions & 0 deletions tests/unit/Model/VirtualTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,15 @@ public function testSetCamelCaseThrowsException()
// @phpstan-ignore-next-line
$virtual->virtualColumn = 'abc';
}

public function testMissingSetVirtualAttributeMethod()
{
$virtual = TestVirtual::new();

$virtual->fromArray([
'only_get' => 'test2',
]);

$this->assertSame('test', $virtual->get('only_get'));
}
}

0 comments on commit f38dc31

Please sign in to comment.