From f97c55eecc53e9027e0bb2782b8a405acb23884c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 4 Mar 2021 15:34:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Database/ActiveRecord.php | 37 ++---------------------------- Database/Base/BaseActiveRecord.php | 28 +++++++++++++++++++++- 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index b410d846..c61ba7f6 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -296,48 +296,15 @@ class ActiveRecord extends BaseActiveRecord private function runRelate(): array { $relates = []; - - echo '--------------->' . PHP_EOL; - var_dump($this->_with, get_called_class()); - echo '<---------------' . PHP_EOL; - - if (empty($this->_with)) { + if (empty($with = $this->getWith())) { return $relates; } - foreach ($this->_with as $val) { + foreach ($with as $val) { $relates[$val] = $this->resolveObject($val); } - $this->_with = []; return $relates; } - private array $_with = []; - - /** - * @param $data - * @return ActiveRecord - */ - public function setWith($data): static - { - echo '--------------->' . PHP_EOL; - var_dump($data, get_called_class()); - echo '<---------------' . PHP_EOL; - if (empty($data)) { - return $this; - } - $this->_with = $data; - return $this; - } - - - /** - * @return array|null - */ - public function getWith(): array|null - { - return $this->_with; - } - /** * @param string $modelName diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index 85631980..e1c1a92e 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -88,6 +88,32 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess protected ?Relation $_relation; + private array $_with = []; + + /** + * @param $data + * @return ActiveRecord + */ + public function setWith($data): static + { + if (empty($data)) { + return $this; + } + $this->_with = $data; + return $this; + } + + + /** + * @return array|null + */ + public function getWith(): array|null + { + return $this->_with; + } + + + /** * @param SEvent $event * 默认注入 @@ -839,7 +865,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess if (array_key_exists($name, $this->_attributes)) { return static::getColumns()->_decode($name, $value); } - if (isset($this->_relate[$name])) { + if (isset($this->_with[$name])) { return $this->resolveClass($this->{$this->_relate[$name]}()); } return parent::__get($name);