diff --git a/src/Annotation/Relation.php b/src/Annotation/Relation.php index 73a11f7..0a5ddef 100644 --- a/src/Annotation/Relation.php +++ b/src/Annotation/Relation.php @@ -17,26 +17,25 @@ use Exception; { - /** - * Relation constructor. - * @param string $name - */ - public function __construct(public string $name) - { - } + /** + * Relation constructor. + * @param string $name + */ + public function __construct(public string $name) + { + } - /** - * @param static $params - * @param mixed $class - * @param mixed|null $method - * @return bool - * @throws Exception - */ - public function execute(mixed $class, mixed $method = null): bool - { - di(Relate::class)->addRelate($class, $this->name, $method); - return true; - } + /** + * @param mixed $class + * @param mixed|null $method + * @return bool + * @throws Exception + */ + public function execute(mixed $class, mixed $method = null): bool + { + di(Relate::class)->addRelate($this->name, $class, $method); + return true; + } } diff --git a/src/Model.php b/src/Model.php index f6310d3..cf66b9a 100644 --- a/src/Model.php +++ b/src/Model.php @@ -274,10 +274,13 @@ class Model extends Base\Model * @return mixed * @throws Exception */ - private function resolveObject($method): mixed + private function withRelation($method): mixed { - var_dump($this->getRelate($method)); - $resolve = $this->{$this->getRelate($method)}(); + $method = $this->getRelate($method); + if (empty($method)) { + return null; + } + $resolve = $this->{$method}(); if ($resolve instanceof HasBase) { $resolve = $resolve->get(); } @@ -302,21 +305,21 @@ class Model extends Base\Model foreach ($lists as $key => $item) { $data[$key] = $this->{$item}($data[$key] ?? null); } - return array_merge($data, $this->runRelate()); + return $this->withRelates($data); } /** + * @param $relates * @return array * @throws Exception */ - private function runRelate(): array + private function withRelates($relates): array { - $relates = []; if (empty($with = $this->getWith())) { return $relates; } foreach ($with as $val) { - $relates[$val] = $this->resolveObject($val); + $relates[$val] = $this->withRelation($val); } return $relates; }