diff --git a/src/AbstractActiveRecord.php b/src/AbstractActiveRecord.php index a0a67b541..cdf091cef 100644 --- a/src/AbstractActiveRecord.php +++ b/src/AbstractActiveRecord.php @@ -200,7 +200,7 @@ public function getPrimaryKey(bool $asArray = false): mixed * * @return array An array of related records indexed by relation names. * - * {@see getRelation()} + * {@see relationQuery()} */ public function getRelatedRecords(): array { @@ -338,7 +338,7 @@ public function link(string $name, ActiveRecordInterface $arClass, array $extraC { $viaClass = null; $viaTable = null; - $relation = $this->getRelation($name); + $relation = $this->relationQuery($name); $via = $relation?->getVia(); if ($via !== null) { @@ -552,7 +552,7 @@ public function resetRelation(string $name): void protected function retrieveRelation(string $name): ActiveRecordInterface|array|null { /** @var ActiveQueryInterface $query */ - $query = $this->getRelation($name); + $query = $this->relationQuery($name); $this->setRelationDependencies($name, $query); @@ -792,7 +792,7 @@ public function unlink(string $name, ActiveRecordInterface $arClass, bool $delet { $viaClass = null; $viaTable = null; - $relation = $this->getRelation($name); + $relation = $this->relationQuery($name); $viaRelation = $relation?->getVia(); if ($viaRelation !== null) { @@ -909,7 +909,7 @@ public function unlinkAll(string $name, bool $delete = false): void { $viaClass = null; $viaTable = null; - $relation = $this->getRelation($name); + $relation = $this->relationQuery($name); $viaRelation = $relation?->getVia(); if ($relation instanceof ActiveQueryInterface && $viaRelation !== null) { diff --git a/src/ActiveQuery.php b/src/ActiveQuery.php index 34cf4e11d..21db7dd73 100644 --- a/src/ActiveQuery.php +++ b/src/ActiveQuery.php @@ -572,7 +572,7 @@ private function joinWithRelations(ActiveRecordInterface $arClass, array $with, $fullName = $prefix === '' ? $name : "$prefix.$name"; if (!isset($relations[$fullName])) { - $relations[$fullName] = $relation = $primaryModel->getRelation($name); + $relations[$fullName] = $relation = $primaryModel->relationQuery($name); if ($relation instanceof ActiveQueryInterface) { $this->joinWithRelation($parent, $relation, $this->getJoinType($joinType, $fullName)); } @@ -592,7 +592,7 @@ private function joinWithRelations(ActiveRecordInterface $arClass, array $with, $fullName = $prefix === '' ? $name : "$prefix.$name"; if (!isset($relations[$fullName])) { - $relations[$fullName] = $relation = $primaryModel->getRelation($name); + $relations[$fullName] = $relation = $primaryModel->relationQuery($name); if ($callback !== null) { $callback($relation); diff --git a/src/ActiveQueryTrait.php b/src/ActiveQueryTrait.php index 59845712f..eed6682a0 100644 --- a/src/ActiveQueryTrait.php +++ b/src/ActiveQueryTrait.php @@ -181,7 +181,7 @@ private function normalizeRelations(ActiveRecordInterface $model, array $with): if (!isset($relations[$name])) { /** @var ActiveQuery $relation */ - $relation = $model->getRelation($name); + $relation = $model->relationQuery($name); $relation->primaryModel = null; $relations[$name] = $relation; } else { diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 62d66067d..46e6a3793 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -199,7 +199,7 @@ public function getPrimaryKey(bool $asArray = false): mixed; * * @return ActiveQueryInterface|null The relational query object. */ - public function getRelation(string $name, bool $throwException = true): ActiveQueryInterface|null; + public function relationQuery(string $name, bool $throwException = true): ActiveQueryInterface|null; /** * Return the name of the table associated with this AR class. @@ -270,7 +270,7 @@ public function isPrimaryKey(array $keys): bool; * * @return bool Whether relation has been populated with records. * - * {@see getRelation()} + * {@see relationQuery()} */ public function isRelationPopulated(string $name): bool; diff --git a/src/ActiveRelationTrait.php b/src/ActiveRelationTrait.php index 93db48525..630d84e61 100644 --- a/src/ActiveRelationTrait.php +++ b/src/ActiveRelationTrait.php @@ -94,7 +94,7 @@ public function __clone() */ public function via(string $relationName, callable $callable = null): static { - $relation = $this->primaryModel?->getRelation($relationName); + $relation = $this->primaryModel?->relationQuery($relationName); $callableUsed = $callable !== null; $this->via = [$relationName, $relation, $callableUsed]; @@ -201,7 +201,7 @@ private function addInverseRelations(array &$result): void if ($relatedModel instanceof ActiveRecordInterface) { if (!isset($inverseRelation)) { /** @var ActiveQuery $inverseRelation */ - $inverseRelation = $relatedModel->getRelation($this->inverseOf); + $inverseRelation = $relatedModel->relationQuery($this->inverseOf); } $relatedModel->populateRelation( $this->inverseOf, @@ -210,7 +210,7 @@ private function addInverseRelations(array &$result): void } else { if (!isset($inverseRelation)) { /** @var ActiveQuery $inverseRelation */ - $inverseRelation = $this->getARInstance()->getRelation($this->inverseOf); + $inverseRelation = $this->getARInstance()->relationQuery($this->inverseOf); } $result[$i][$this->inverseOf] = $inverseRelation->multiple @@ -359,10 +359,10 @@ private function populateInverseRelation( if ($model instanceof ActiveRecordInterface) { /** @var ActiveQuery $relation */ - $relation = $model->getRelation($name); + $relation = $model->relationQuery($name); } else { /** @var ActiveQuery $relation */ - $relation = $this->getARInstance()->getRelation($name); + $relation = $this->getARInstance()->relationQuery($name); } if ($relation->getMultiple()) { diff --git a/src/Trait/MagicRelationsTrait.php b/src/Trait/MagicRelationsTrait.php index b055f5882..a2ca973dd 100644 --- a/src/Trait/MagicRelationsTrait.php +++ b/src/Trait/MagicRelationsTrait.php @@ -17,7 +17,7 @@ use function ucfirst; /** - * Trait to define {@see ActiveRecordInterface::getRelation()} method to access relation queries of an ActiveRecord + * Trait to define {@see ActiveRecordInterface::relationQuery()} method to access relation queries of an ActiveRecord * instance. */ trait MagicRelationsTrait @@ -39,7 +39,7 @@ trait MagicRelationsTrait * @return ActiveQueryInterface|null the relational query object. If the relation does not exist and * `$throwException` is `false`, `null` will be returned. */ - public function getRelation(string $name, bool $throwException = true): ActiveQueryInterface|null + public function relationQuery(string $name, bool $throwException = true): ActiveQueryInterface|null { $getter = 'get' . ucfirst($name); diff --git a/tests/ActiveQueryTest.php b/tests/ActiveQueryTest.php index 0f7f019de..d3aacc1e5 100644 --- a/tests/ActiveQueryTest.php +++ b/tests/ActiveQueryTest.php @@ -2332,14 +2332,14 @@ public function testGetRelationInvalidArgumentException(): void $query = $customer->findOne(1); /** Without throwing exception */ - $this->assertEmpty($query->getRelation('items', false)); + $this->assertEmpty($query->relationQuery('items', false)); /** Throwing exception */ $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( 'Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer has no relation named "items".' ); - $query->getRelation('items'); + $query->relationQuery('items'); } public function testGetRelationInvalidArgumentExceptionHasNoRelationNamed(): void @@ -2351,13 +2351,13 @@ public function testGetRelationInvalidArgumentExceptionHasNoRelationNamed(): voi $query = $customer->findOne(1); /** Without throwing exception */ - $this->assertEmpty($query->getRelation('item', false)); + $this->assertEmpty($query->relationQuery('item', false)); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( 'Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer has no relation named "item"' ); - $query->getRelation('item'); + $query->relationQuery('item'); } public function testGetRelationInvalidArgumentExceptionCaseSensitive(): void @@ -2368,14 +2368,14 @@ public function testGetRelationInvalidArgumentExceptionCaseSensitive(): void $query = $customer->findOne(1); - $this->assertEmpty($query->getRelation('expensiveorders', false)); + $this->assertEmpty($query->relationQuery('expensiveorders', false)); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage( 'Relation names are case sensitive. Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Customer ' . 'has a relation named "expensiveOrders" instead of "expensiveorders"' ); - $query->getRelation('expensiveorders'); + $query->relationQuery('expensiveorders'); } public function testExists(): void