Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 10 additions & 0 deletions src/AbstractActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,16 @@ public function hasProperty(string $name): bool
return in_array($name, $this->propertyNames(), true);
}

/**
* 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 false;
}

/**
* Declares a `has-many` relation.
*
Expand Down
8 changes: 8 additions & 0 deletions src/ActiveRecordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ public function tableName(): string;
*/
public function hasProperty(string $name): bool;

/**
* Returns a value indicating whether the record has a relation query with the specified name.
*
* @param string $name The name of the relation query.
* @return bool True if the relation query exists, false otherwise.
*/
public function hasRelationQuery(string $name): bool;

/**
* Inserts a row into the associated database table using the property values of this record.
* You may specify the properties to be inserted as list of name or name-value pairs.
Expand Down
11 changes: 7 additions & 4 deletions src/Trait/MagicPropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
* @method bool hasDependentRelations(string $propertyName)
* @see AbstractActiveRecord::hasDependentRelations()
*
* @method bool hasRelationQuery(string $name)
* @see ActiveRecordInterface::hasRelationQuery()
*
* @method bool isRelationPopulated(string $name)
* @see ActiveRecordInterface::isRelationPopulated()
*
Expand Down Expand Up @@ -74,7 +77,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 +145,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 +188,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
12 changes: 12 additions & 0 deletions src/Trait/MagicRelationsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
*/
trait MagicRelationsTrait
{
use MagicPropertiesTrait;

/**
* 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");
}

/**
* @inheritdoc
*
Expand Down
17 changes: 17 additions & 0 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,23 @@ 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('ordersNoOrder'));
$this->assertFalse($customer->hasRelationQuery('expensiveOrders'));

$customerQuery = new ActiveQuery(Customer::class);
$customer = $customerQuery->findByPk(1);
$this->assertTrue($customer->hasRelationQuery('profile'));
$this->assertTrue($customer->hasRelationQuery('ordersPlain'));
$this->assertFalse($customer->hasRelationQuery('ordersNoOrder'));
$this->assertFalse($customer->hasRelationQuery('expensiveOrders'));
}

public function testRefresh(): void
{
$customer = new Customer();
Expand Down
Loading