diff --git a/src/AbstractActiveRecord.php b/src/AbstractActiveRecord.php index cdf091cef..1617311f3 100644 --- a/src/AbstractActiveRecord.php +++ b/src/AbstractActiveRecord.php @@ -540,6 +540,15 @@ public function refresh(): bool return $this->refreshInternal($record); } + public function relation(string $name): ActiveRecordInterface|array|null + { + if (array_key_exists($name, $this->related)) { + return $this->related[$name]; + } + + return $this->retrieveRelation($name); + } + public function resetRelation(string $name): void { foreach ($this->relationsDependencies as &$relationNames) { diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index 46e6a3793..64a9558f9 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -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): ActiveRecordInterface|array|null; + /** * Returns the relation object with the specified name. *