Skip to content

Commit 107cd30

Browse files
authored
Add AbstractActiveRecord::relationQuery() default method (#323)
1 parent 13fa9d3 commit 107cd30

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

src/AbstractActiveRecord.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,31 @@ public function relation(string $name): ActiveRecordInterface|array|null
549549
return $this->retrieveRelation($name);
550550
}
551551

552+
/**
553+
* @inheritdoc
554+
*
555+
* Relations can be defined using {@see hasOne()} and {@see hasMany()} methods. For example:
556+
*
557+
* ```php
558+
* public function relationQuery(string $name): ActiveQueryInterface
559+
* {
560+
* return match ($name) {
561+
* 'orders' => $this->hasMany(Order::class, ['customer_id' => 'id']),
562+
* 'country' => $this->hasOne(Country::class, ['id' => 'country_id']),
563+
* default => parent::relationQuery($name),
564+
* };
565+
* }
566+
* ```
567+
*/
568+
public function relationQuery(string $name, bool $throwException = true): ActiveQueryInterface|null
569+
{
570+
if (!$throwException) {
571+
return null;
572+
}
573+
574+
throw new InvalidArgumentException(static::class . ' has no relation named "' . $name . '".');
575+
}
576+
552577
public function resetRelation(string $name): void
553578
{
554579
foreach ($this->relationsDependencies as &$relationNames) {

0 commit comments

Comments
 (0)