Skip to content

Commit 13fa9d3

Browse files
authored
Add ActiveRecordInterface::relation() method (#322)
1 parent dde6315 commit 13fa9d3

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/AbstractActiveRecord.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,15 @@ public function refresh(): bool
540540
return $this->refreshInternal($record);
541541
}
542542

543+
public function relation(string $name): ActiveRecordInterface|array|null
544+
{
545+
if (array_key_exists($name, $this->related)) {
546+
return $this->related[$name];
547+
}
548+
549+
return $this->retrieveRelation($name);
550+
}
551+
543552
public function resetRelation(string $name): void
544553
{
545554
foreach ($this->relationsDependencies as &$relationNames) {

src/ActiveRecordInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ public function getOldPrimaryKey(bool $asArray = false): mixed;
187187
*/
188188
public function getPrimaryKey(bool $asArray = false): mixed;
189189

190+
/**
191+
* Returns the relation object with the specified name.
192+
*
193+
* @param string $name The relation name (case-sensitive).
194+
*
195+
* @return ActiveRecordInterface|array|null The relation object.
196+
*/
197+
public function relation(string $name): self|array|null;
198+
190199
/**
191200
* Returns the relation object with the specified name.
192201
*

0 commit comments

Comments
 (0)