diff --git a/Base/Model.php b/Base/Model.php index b0ed56e..3aba07d 100644 --- a/Base/Model.php +++ b/Base/Model.php @@ -108,28 +108,34 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T /** - * @param $data + * @param array $data * @return Model */ - public function setWith($data): static + public function setWith(array $data): static { - if (empty($data)) { - return $this; - } $this->_with = $data; return $this; } /** - * @return array|null + * @return array */ - public function getWith(): array|null + public function getWith(): array { return $this->_with; } + /** + * @return bool + */ + public function hasWith(): bool + { + return count($this->_with) > 0; + } + + /** * object init */ diff --git a/Model.php b/Model.php index af0699f..25c7674 100644 --- a/Model.php +++ b/Model.php @@ -265,8 +265,8 @@ class Model extends Base\Model public function toArray(): array { $data = $this->_attributes; - $keys = Kiri::getDi()->get(Getter::class); - foreach ($keys->getAll(static::class) as $key => $datum) { + $keys = Kiri::getDi()->get(Getter::class)->getAll(static::class); + foreach ($keys as $key => $datum) { $data[$key] = $this->{$datum}($data[$key]); } return $this->withRelates($data); @@ -279,10 +279,10 @@ class Model extends Base\Model */ private function withRelates($relates): array { - if (empty($with = $this->getWith())) { + if (!$this->hasWith()) { return $relates; } - foreach ($with as $val) { + foreach ($this->getWith() as $val) { $relates[$val] = $this->withRelate($val); } return $relates;