Skip to content

Commit 7b74a7b

Browse files
authored
Add function hasRelationQuery(string $name): bool for ActiveRecordInt… (#436)
1 parent de8fb61 commit 7b74a7b

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/Trait/MagicPropertiesTrait.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ trait MagicPropertiesTrait
4545
/** @psalm-var array<string, mixed> $propertyValues */
4646
private array $propertyValues = [];
4747

48+
/**
49+
* Returns a value indicating whether the record has a relation query with the specified name.
50+
*
51+
* @param string $name The name of the relation query.
52+
*/
53+
public function hasRelationQuery(string $name): bool
54+
{
55+
return method_exists($this, "get{$name}Query");
56+
}
57+
4858
/**
4959
* PHP getter magic method.
5060
* This method is overridden so that values and related objects can be accessed like properties.
@@ -74,7 +84,7 @@ public function __get(string $name)
7484
return $this->relatedRecords()[$name];
7585
}
7686

77-
if (method_exists($this, "get{$name}Query")) {
87+
if ($this->hasRelationQuery($name)) {
7888
/** Read relation query getter, e.g., getUserQuery() */
7989
return $this->retrieveRelation($name);
8090
}
@@ -142,7 +152,7 @@ public function __set(string $name, mixed $value): void
142152

143153
if (
144154
method_exists($this, "get$name")
145-
|| method_exists($this, "get{$name}Query")
155+
|| $this->hasRelationQuery($name)
146156
) {
147157
throw new InvalidCallException('Setting read-only property: ' . static::class . '::' . $name);
148158
}
@@ -185,15 +195,15 @@ public function isProperty(string $name, bool $checkVars = true): bool
185195
{
186196
return method_exists($this, "get$name")
187197
|| method_exists($this, "set$name")
188-
|| method_exists($this, "get{$name}Query")
198+
|| $this->hasRelationQuery($name)
189199
|| ($checkVars && property_exists($this, $name))
190200
|| $this->hasProperty($name);
191201
}
192202

193203
public function canGetProperty(string $name, bool $checkVars = true): bool
194204
{
195205
return method_exists($this, "get$name")
196-
|| method_exists($this, "get{$name}Query")
206+
|| $this->hasRelationQuery($name)
197207
|| ($checkVars && property_exists($this, $name))
198208
|| $this->hasProperty($name);
199209
}

tests/MagicActiveRecordTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,15 @@ public function testHasProperty(): void
522522
$this->assertFalse($customer->hasProperty('notExist'));
523523
}
524524

525+
public function testHasRelationQuery(): void
526+
{
527+
$customer = new Customer();
528+
529+
$this->assertTrue($customer->hasRelationQuery('profile'));
530+
$this->assertTrue($customer->hasRelationQuery('ordersPlain'));
531+
$this->assertFalse($customer->hasRelationQuery('nonExistsRelation'));
532+
}
533+
525534
public function testRefresh(): void
526535
{
527536
$customer = new Customer();

0 commit comments

Comments
 (0)