From 7112389ca734c15fdaacc2f3f67fc5098269d338 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 8 Apr 2016 11:10:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=87=AA=E5=8A=A8=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E6=9C=BA=E5=88=B6=20=E5=8F=AA=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=20=E6=96=B0=E5=A2=9E=E6=88=96=E8=80=85?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=9C=80=E8=A6=81=E8=87=AA=E5=8A=A8=E5=A4=84?= =?UTF-8?q?=E7=90=86=E7=9A=84=E5=AD=97=E6=AE=B5=E5=90=8D=E5=88=97=E8=A1=A8?= =?UTF-8?q?=20=E7=84=B6=E5=90=8E=E4=BA=A4=E7=BB=99=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=99=A8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 95 +++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 57 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index cc34c1fe..7e9fa9fe 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -55,10 +55,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $createTimeField = 'create_time'; // 更新时间戳字段 protected $updateTimeField = 'update_time'; + // 时间戳字段列表 + protected $timestampField = []; - // 新增的字段完成 + // 新增要自动完成的字段列表 protected $insert = []; - // 更新的字段完成 + // 更新要自动完成的字段列表 protected $update = []; // 字段类型或者格式转换 @@ -307,10 +309,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $this->__set($key, $value); } } - $data = $this->data; // 数据自动验证 - if (!$this->dataValidate($data)) { + if (!$this->dataValidate($this->data)) { return false; } @@ -319,42 +320,45 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } // 数据自动完成 - foreach ($this->auto as $name => $rule) { - if (!in_array($name, $this->change)) { - $this->change[] = $name; - $data[$name] = $this->auto($name, $rule, $data); + foreach ($this->auto as $field) { + if (!in_array($field, $this->change)) { + // 没有经过修改器赋值则进行自动完成 + $this->__set($field, isset($this->data[$field]) ? $this->data[$field] : null); } } if ($this->timestamps) { - $data[$this->updateTimeField] = NOW_TIME; + // 自动更新时间戳 + $this->data[$this->updateTimeField] = NOW_TIME; } + // 检测是否为更新数据 - if ($this->isUpdate($data)) { + if ($this->isUpdate()) { if (false === $this->trigger('before_update', $this)) { return false; } - // 去除没有更新的字段 - foreach ($data as $key => $val) { - if (!in_array($key, $this->change) && !$this->isPk($key) && !isset($this->relation[$key])) { - unset($data[$key]); + + // 自动更新 + foreach ($this->update as $field) { + if (!in_array($field, $this->change)) { + $this->__set($field, isset($this->data[$field]) ? $this->data[$field] : null); } } - // 自动更新 - foreach ($this->update as $name => $rule) { - $data[$name] = $this->auto($name, $rule, $data); + // 去除没有更新的字段 + foreach ($this->data as $key => $val) { + if (!in_array($key, $this->change) && !$this->isPk($key) && !isset($this->relation[$key])) { + unset($this->data[$key]); + } } $db = self::db(); if (!empty($where)) { $db->where($where); } - $result = $db->update($data); + $result = $db->update($this->data); - // 赋值数据对象值 - $this->data = $data; // 更新回调 $this->trigger('after_update', $this); @@ -365,26 +369,27 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } if ($this->timestamps) { - $data[$this->createTimeField] = NOW_TIME; + // 自动写入时间戳 + $this->data[$this->createTimeField] = NOW_TIME; } // 自动写入 - foreach ($this->insert as $name => $rule) { - $data[$name] = $this->auto($name, $rule, $data); + foreach ($this->insert as $field) { + if (!in_array($field, $this->change)) { + $this->__set($field, isset($this->data[$field]) ? $this->data[$field] : null); + } } - $result = self::db()->insert($data); + $result = self::db()->insert($this->data); // 获取自动增长主键 if ($result) { $insertId = self::db()->getLastInsID(); if (is_string($this->pk) && $insertId) { - $data[$this->pk] = $insertId; + $this->data[$this->pk] = $insertId; } } - // 数据对象赋值 - $this->data = $data; // 新增回调 $this->trigger('after_insert', $this); @@ -396,28 +401,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $result; } - /** - * 数据自动完成 - * @access protected - * @return mixed - */ - protected function auto($key, $val, &$data) - { - $value = isset($data[$key]) ? $data[$key] : null; - $rule = isset($val[0]) ? $val[0] : $val; - $type = isset($val[1]) ? $val[1] : 'value'; - switch ($type) { - case 'callback': - $result = call_user_func_array($rule, [$value, &$data]); - break; - case 'value': - default: - $result = $rule; - break; - } - return $result; - } - /** * 删除当前的记录 * @access public @@ -425,13 +408,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function delete() { - $data = $this->data; - if (false === $this->trigger('before_delete', $this)) { return false; } - $result = self::db()->delete($data); + $result = self::db()->delete($this->data); $this->trigger('after_delete', $this); return $result; @@ -493,13 +474,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess /** * 是否为数据库更新操作 * @access public - * @param mixed $data 数据 + * @param bool|null $update 是否为更新数据 * @return bool */ - public function isUpdate($data = null) + public function isUpdate($update = null) { - if (is_bool($data)) { - $this->isUpdate = $data; + if (is_bool($update)) { + $this->isUpdate = $update; return $this; } @@ -509,7 +490,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } // 根据主键判断是否更新 - $data = $data ?: $this->data; + $data = $this->data; $pk = $this->pk; if (is_string($pk) && isset($data[$pk])) { return true;