This commit is contained in:
2023-04-01 01:27:57 +08:00
parent 98fff7302e
commit e2f94c9202
2 changed files with 17 additions and 11 deletions
+13 -7
View File
@@ -108,28 +108,34 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T
/** /**
* @param $data * @param array $data
* @return Model * @return Model
*/ */
public function setWith($data): static public function setWith(array $data): static
{ {
if (empty($data)) {
return $this;
}
$this->_with = $data; $this->_with = $data;
return $this; return $this;
} }
/** /**
* @return array|null * @return array
*/ */
public function getWith(): array|null public function getWith(): array
{ {
return $this->_with; return $this->_with;
} }
/**
* @return bool
*/
public function hasWith(): bool
{
return count($this->_with) > 0;
}
/** /**
* object init * object init
*/ */
+4 -4
View File
@@ -265,8 +265,8 @@ class Model extends Base\Model
public function toArray(): array public function toArray(): array
{ {
$data = $this->_attributes; $data = $this->_attributes;
$keys = Kiri::getDi()->get(Getter::class); $keys = Kiri::getDi()->get(Getter::class)->getAll(static::class);
foreach ($keys->getAll(static::class) as $key => $datum) { foreach ($keys as $key => $datum) {
$data[$key] = $this->{$datum}($data[$key]); $data[$key] = $this->{$datum}($data[$key]);
} }
return $this->withRelates($data); return $this->withRelates($data);
@@ -279,10 +279,10 @@ class Model extends Base\Model
*/ */
private function withRelates($relates): array private function withRelates($relates): array
{ {
if (empty($with = $this->getWith())) { if (!$this->hasWith()) {
return $relates; return $relates;
} }
foreach ($with as $val) { foreach ($this->getWith() as $val) {
$relates[$val] = $this->withRelate($val); $relates[$val] = $this->withRelate($val);
} }
return $relates; return $relates;