From 2283833773a24a0092e9a59781856122bffccc16 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 13 Dec 2023 14:54:58 +0700 Subject: [PATCH] Fix setting value for an attribute when AR class has get method for the attribute --- src/BaseActiveRecordTrait.php | 4 +--- tests/Stubs/ActiveRecord/Customer.php | 5 +++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/BaseActiveRecordTrait.php b/src/BaseActiveRecordTrait.php index c5061b1ac..966e6e429 100644 --- a/src/BaseActiveRecordTrait.php +++ b/src/BaseActiveRecordTrait.php @@ -200,9 +200,7 @@ public function __set(string $name, mixed $value): void $this->resetDependentRelations($name); } $this->attributes[$name] = $value; - } - - if (method_exists($this, 'get' . ucfirst($name))) { + } elseif (method_exists($this, 'get' . ucfirst($name))) { throw new InvalidCallException('Setting read-only property: ' . static::class . '::' . $name); } } diff --git a/tests/Stubs/ActiveRecord/Customer.php b/tests/Stubs/ActiveRecord/Customer.php index 9029d7072..1688819f6 100644 --- a/tests/Stubs/ActiveRecord/Customer.php +++ b/tests/Stubs/ActiveRecord/Customer.php @@ -37,6 +37,11 @@ public function getTableName(): string return 'customer'; } + public function getName(): string + { + return $this->name; + } + public function getProfile(): ActiveQuery { return $this->hasOne(Profile::class, ['id' => 'profile_id']);