Skip to content

Commit

Permalink
Add AbstractActiveRecord::relationQuery() default method (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored May 20, 2024
1 parent 13fa9d3 commit 107cd30
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
{
if (!$throwException) {
return null;
}

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

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

0 comments on commit 107cd30

Please sign in to comment.