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
+18 -19
View File
@@ -17,26 +17,25 @@ use Exception;
{ {
/** /**
* Relation constructor. * Relation constructor.
* @param string $name * @param string $name
*/ */
public function __construct(public string $name) public function __construct(public string $name)
{ {
} }
/** /**
* @param static $params * @param mixed $class
* @param mixed $class * @param mixed|null $method
* @param mixed|null $method * @return bool
* @return bool * @throws Exception
* @throws Exception */
*/ public function execute(mixed $class, mixed $method = null): bool
public function execute(mixed $class, mixed $method = null): bool {
{ di(Relate::class)->addRelate($this->name, $class, $method);
di(Relate::class)->addRelate($class, $this->name, $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;
} }