Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/Trait/MagicPropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ trait MagicPropertiesTrait
/** @psalm-var array<string, mixed> $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.
Expand Down Expand Up @@ -74,7 +84,7 @@ public function __get(string $name)
return $this->relatedRecords()[$name];
}

if (method_exists($this, "get{$name}Query")) {
if ($this->hasRelationQuery($name)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth checking performance after this change.

/** Read relation query getter, e.g., getUserQuery() */
return $this->retrieveRelation($name);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -185,15 +195,15 @@ 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);
}

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);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/MagicActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading