diff --git a/src/Trait/MagicPropertiesTrait.php b/src/Trait/MagicPropertiesTrait.php index 2de028c25..b7d8d5868 100644 --- a/src/Trait/MagicPropertiesTrait.php +++ b/src/Trait/MagicPropertiesTrait.php @@ -45,6 +45,16 @@ trait MagicPropertiesTrait /** @psalm-var array $propertyValues */ private array $propertyValues = []; + /** + * Returns a value indicating whether the record has a relation query with the specified name. + * + * @param string $name The name of the relation query. + */ + public function hasRelationQuery(string $name): bool + { + return method_exists($this, "get{$name}Query"); + } + /** * PHP getter magic method. * This method is overridden so that values and related objects can be accessed like properties. @@ -74,7 +84,7 @@ public function __get(string $name) return $this->relatedRecords()[$name]; } - if (method_exists($this, "get{$name}Query")) { + if ($this->hasRelationQuery($name)) { /** Read relation query getter, e.g., getUserQuery() */ return $this->retrieveRelation($name); } @@ -142,7 +152,7 @@ public function __set(string $name, mixed $value): void if ( method_exists($this, "get$name") - || method_exists($this, "get{$name}Query") + || $this->hasRelationQuery($name) ) { throw new InvalidCallException('Setting read-only property: ' . static::class . '::' . $name); } @@ -185,7 +195,7 @@ public function isProperty(string $name, bool $checkVars = true): bool { return method_exists($this, "get$name") || method_exists($this, "set$name") - || method_exists($this, "get{$name}Query") + || $this->hasRelationQuery($name) || ($checkVars && property_exists($this, $name)) || $this->hasProperty($name); } @@ -193,7 +203,7 @@ public function isProperty(string $name, bool $checkVars = true): bool public function canGetProperty(string $name, bool $checkVars = true): bool { return method_exists($this, "get$name") - || method_exists($this, "get{$name}Query") + || $this->hasRelationQuery($name) || ($checkVars && property_exists($this, $name)) || $this->hasProperty($name); } diff --git a/tests/MagicActiveRecordTest.php b/tests/MagicActiveRecordTest.php index 0f8fb46be..e79baeb22 100644 --- a/tests/MagicActiveRecordTest.php +++ b/tests/MagicActiveRecordTest.php @@ -522,6 +522,15 @@ public function testHasProperty(): void $this->assertFalse($customer->hasProperty('notExist')); } + public function testHasRelationQuery(): void + { + $customer = new Customer(); + + $this->assertTrue($customer->hasRelationQuery('profile')); + $this->assertTrue($customer->hasRelationQuery('ordersPlain')); + $this->assertFalse($customer->hasRelationQuery('nonExistsRelation')); + } + public function testRefresh(): void { $customer = new Customer();