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 ActiveRecordInterface::relation() method #322

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions src/AbstractActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,15 @@
return $this->refreshInternal($record);
}

public function relation(string $name): ActiveRecordInterface|array|null

Check warning on line 543 in src/AbstractActiveRecord.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractActiveRecord.php#L543

Added line #L543 was not covered by tests
{
if (array_key_exists($name, $this->related)) {
return $this->related[$name];

Check warning on line 546 in src/AbstractActiveRecord.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractActiveRecord.php#L545-L546

Added lines #L545 - L546 were not covered by tests
}

return $this->retrieveRelation($name);

Check warning on line 549 in src/AbstractActiveRecord.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractActiveRecord.php#L549

Added line #L549 was not covered by tests
}

public function resetRelation(string $name): void
{
foreach ($this->relationsDependencies as &$relationNames) {
Expand Down
9 changes: 9 additions & 0 deletions src/ActiveRecordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ public function getOldPrimaryKey(bool $asArray = false): mixed;
*/
public function getPrimaryKey(bool $asArray = false): mixed;

/**
* Returns the relation object with the specified name.
*
* @param string $name The relation name (case-sensitive).
*
* @return ActiveRecordInterface|array|null The relation object.
*/
public function relation(string $name): self|array|null;

/**
* Returns the relation object with the specified name.
*
Expand Down
Loading