Skip to content

Commit

Permalink
Add AbstractActiveRecord::relationQuery() default method
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed May 20, 2024
1 parent 13fa9d3 commit 39d3e71
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/AbstractActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,31 @@ public function relation(string $name): ActiveRecordInterface|array|null
return $this->retrieveRelation($name);
}

/**
* @inheritdoc
*
* Relations can be defined using {@see hasOne()} and {@see hasMany()} methods. For example:
*
* ```php
* public function relationQuery(string $name): ActiveQueryInterface
* {
* return match ($name) {
* 'orders' => $this->hasMany(Order::class, ['customer_id' => 'id']),
* 'country' => $this->hasOne(Country::class, ['id' => 'country_id']),
* default => parent::relationQuery($name),
* };
* }
* ```
*/
public function relationQuery(string $name, bool $throwException = true): ActiveQueryInterface|null

Check warning on line 568 in src/AbstractActiveRecord.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractActiveRecord.php#L568

Added line #L568 was not covered by tests
{
if (!$throwException) {
return null;

Check warning on line 571 in src/AbstractActiveRecord.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractActiveRecord.php#L570-L571

Added lines #L570 - L571 were not covered by tests
}

throw new InvalidArgumentException(static::class . ' has no relation named "' . $name . '".');

Check warning on line 574 in src/AbstractActiveRecord.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractActiveRecord.php#L574

Added line #L574 was not covered by tests
}

public function resetRelation(string $name): void
{
foreach ($this->relationsDependencies as &$relationNames) {
Expand Down

0 comments on commit 39d3e71

Please sign in to comment.