Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AbstractActiveRecord::relationQuery() default method #323

Merged
merged 1 commit into from
May 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
Loading