This commit is contained in:
2021-10-19 18:36:35 +08:00
parent d51555dfe8
commit a6e5d70b96
2 changed files with 28 additions and 26 deletions
+1 -2
View File
@@ -27,7 +27,6 @@ use Exception;
/** /**
* @param static $params
* @param mixed $class * @param mixed $class
* @param mixed|null $method * @param mixed|null $method
* @return bool * @return bool
@@ -35,7 +34,7 @@ use Exception;
*/ */
public function execute(mixed $class, mixed $method = null): bool public function execute(mixed $class, mixed $method = null): bool
{ {
di(Relate::class)->addRelate($class, $this->name, $method); di(Relate::class)->addRelate($this->name, $class, $method);
return true; return true;
} }
+10 -7
View File
@@ -274,10 +274,13 @@ class Model extends Base\Model
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
private function resolveObject($method): mixed private function withRelation($method): mixed
{ {
var_dump($this->getRelate($method)); $method = $this->getRelate($method);
$resolve = $this->{$this->getRelate($method)}(); if (empty($method)) {
return null;
}
$resolve = $this->{$method}();
if ($resolve instanceof HasBase) { if ($resolve instanceof HasBase) {
$resolve = $resolve->get(); $resolve = $resolve->get();
} }
@@ -302,21 +305,21 @@ class Model extends Base\Model
foreach ($lists as $key => $item) { foreach ($lists as $key => $item) {
$data[$key] = $this->{$item}($data[$key] ?? null); $data[$key] = $this->{$item}($data[$key] ?? null);
} }
return array_merge($data, $this->runRelate()); return $this->withRelates($data);
} }
/** /**
* @param $relates
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
private function runRelate(): array private function withRelates($relates): array
{ {
$relates = [];
if (empty($with = $this->getWith())) { if (empty($with = $this->getWith())) {
return $relates; return $relates;
} }
foreach ($with as $val) { foreach ($with as $val) {
$relates[$val] = $this->resolveObject($val); $relates[$val] = $this->withRelation($val);
} }
return $relates; return $relates;
} }