From f162cbc44fc1e04d09c0d01b008f26b954a55eb2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 7 Apr 2016 12:36:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BisUpdate=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 558ebb98..16d71356 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -59,6 +59,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 当前执行的关联类型 private $relation; + // 是否为更新 + protected $isUpdate = null; /** * 架构函数 @@ -67,7 +69,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function __construct($data = []) { - $this->data = $data; + if (empty($data)) { + $this->isUpdate = false; + } else { + $this->data = $data; + } $this->name = basename(str_replace('\\', '/', get_class($this))); } @@ -112,6 +118,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess throw new Exception('data type invalid', 10300); } $this->data = $data; + // 标记为新增数据 + $this->isUpdate = false; return $this; } @@ -245,6 +253,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function save($data = [], $where = []) { if (!empty($data)) { + // 标记为更新数据 + $this->isUpdate = true; foreach ($data as $key => $value) { $this->__set($key, $value); } @@ -405,16 +415,26 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } /** - * 是否为更新操作 + * 是否为数据库更新操作 * @access public * @param mixed $data 数据 * @return bool */ - public function isUpdate($data = []) + public function isUpdate($data = null) { + if (is_bool($data)) { + $this->isUpdate = $data; + return $this; + } + + // 检测isUpdate属性 + if (isset($this->isUpdate)) { + return $this->isUpdate; + } + + // 根据主键判断是否更新 $data = $data ?: $this->data; - // 判断主键 - $pk = $this->pk; + $pk = $this->pk; if (is_string($pk) && isset($data[$pk])) { return true; } elseif (is_array($pk)) { @@ -491,12 +511,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $guid = 'model_' . $name . '_' . $data; $result = Cache::get($guid); if ($result) { - return new static($result); + $model = new static($result); + $model->isUpdate(true); + return $model; } } $result = self::db()->find($data); + if ($result) { + // 标记为更新数据 + $result->isUpdate(true); + } if ($cache) { // 缓存模型数据 Cache::set($guid, $result->toArray());