From 2283833773a24a0092e9a59781856122bffccc16 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 13 Dec 2023 14:54:58 +0700 Subject: [PATCH 1/3] 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']); From 1af89936d4df04abe57c6ed631756bdf617a9688 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 13 Dec 2023 15:44:15 +0700 Subject: [PATCH 2/3] Update --- src/BaseActiveRecordTrait.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/BaseActiveRecordTrait.php b/src/BaseActiveRecordTrait.php index 966e6e429..ad4166c89 100644 --- a/src/BaseActiveRecordTrait.php +++ b/src/BaseActiveRecordTrait.php @@ -200,7 +200,10 @@ public function __set(string $name, mixed $value): void $this->resetDependentRelations($name); } $this->attributes[$name] = $value; - } elseif (method_exists($this, 'get' . ucfirst($name))) { + return; + } + + if (method_exists($this, 'get' . ucfirst($name))) { throw new InvalidCallException('Setting read-only property: ' . static::class . '::' . $name); } } From 4a65d019361f1a1f3e55502c183d3334b967e6f5 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 16 Dec 2023 09:47:23 +0700 Subject: [PATCH 3/3] Change `$this->name` to `$this->getAttribute('name')` --- tests/Stubs/ActiveRecord/Customer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Stubs/ActiveRecord/Customer.php b/tests/Stubs/ActiveRecord/Customer.php index 1688819f6..02055d2a7 100644 --- a/tests/Stubs/ActiveRecord/Customer.php +++ b/tests/Stubs/ActiveRecord/Customer.php @@ -39,7 +39,7 @@ public function getTableName(): string public function getName(): string { - return $this->name; + return $this->getAttribute('name'); } public function getProfile(): ActiveQuery