From e248bbbea06482a6429de19bcf0d240fed5c796c Mon Sep 17 00:00:00 2001 From: whwyy Date: Thu, 14 Dec 2023 09:50:49 +0800 Subject: [PATCH] eee --- Base/Model.php | 24 +++++++++++++----------- Model.php | 12 +----------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/Base/Model.php b/Base/Model.php index d6d5146..e5a2724 100644 --- a/Base/Model.php +++ b/Base/Model.php @@ -483,9 +483,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \ return FALSE; } if (!$this->isNewExample) { - [$changes, $condition] = $this->diff(); - - return $this->updateInternal($condition, $condition, $changes); + return $this->updateInternal(...$this->arrayIntersect($this->_attributes)); } else { return $this->insert(); } @@ -493,18 +491,22 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \ /** - * @return array + * @return array * @throws */ - private function diff(): array + protected function arrayIntersect(array $params): array { - $changes = \array_diff_assoc($this->_attributes, $this->_oldAttributes); - if ($this->hasPrimary()) { - $condition = [$this->primary => $this->_oldAttributes[$this->primary]]; - } else { - $condition = \array_intersect_assoc($this->_oldAttributes, $this->_attributes); + $condition = []; + $oldPrams = []; + foreach ($this->_oldAttributes as $key => $attribute) { + if (!array_key_exists($key, $params) || $params[$key] == $attribute) { + $condition[$key] = $attribute; + unset($params[$key]); + } else { + $oldPrams[$key] = $this->_oldAttributes[$attribute]; + } } - return [$changes, $condition]; + return [$oldPrams, $condition, $params]; } diff --git a/Model.php b/Model.php index ffba292..45fcbe3 100644 --- a/Model.php +++ b/Model.php @@ -195,17 +195,7 @@ class Model extends Base\Model if (!$this->validator($this->rules()) || !$this->beforeSave($this)) { return FALSE; } - - $condition = []; - $oldPrams = []; - foreach ($this->_oldAttributes as $key => $attribute) { - if (!array_key_exists($key, $params) || $params[$key] == $attribute) { - $condition[$key] = $attribute; - } else { - $oldPrams[$key] = $this->_oldAttributes[$attribute]; - } - } - return $this->updateInternal($oldPrams, $condition, $params); + return $this->updateInternal(...$this->arrayIntersect($params)); }