Skip to content

Commit

Permalink
Rename getRelation() to relationQuery()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed May 20, 2024
1 parent d2535c8 commit 03a0b6c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
10 changes: 5 additions & 5 deletions src/AbstractActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/ActiveQueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/ActiveRecordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand Down
10 changes: 5 additions & 5 deletions src/ActiveRelationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Trait/MagicRelationsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down
12 changes: 6 additions & 6 deletions tests/ActiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 03a0b6c

Please sign in to comment.