From 67500bc3c95f86e016f39893343266280c5a1182 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 18 May 2016 18:46:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB=E7=9A=84?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=97=B6=E9=97=B4=E6=88=B3=E5=86=99=E5=85=A5?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=20=E6=97=B6=E9=97=B4=E6=88=B3=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=86=99=E5=85=A5=20=E5=92=8C=20=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=AE=8C=E6=88=90=20=E5=88=86=E5=BC=80=20?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E4=B8=94=E6=94=AF=E6=8C=81=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E5=85=B3=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 2179920d..b0a2e28b 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -59,11 +59,16 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $insert = []; // 更新自动完成列表 protected $update = []; - // 自动写入的时间戳字段列表 - protected $autoTimeField = ['create_time', 'update_time', 'delete_time']; + // 是否需要自动写入时间戳 + protected $autoWriteTimestamp = true; + // 创建时间字段 + protected $createTime = 'create_time'; + // 更新时间字段 + protected $updateTime = 'update_time'; + // 删除时间字段 + protected $deleteTime = 'delete_time'; // 时间字段取出后的默认时间格式 protected $dateFormat = 'Y-m-d H:i:s'; - // 字段类型或者格式转换 protected $type = []; // 是否为更新数据 @@ -286,6 +291,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 数据自动完成 $this->autoCompleteData($this->auto); + // 自动写入更新时间 + if ($this->autoWriteTimestamp) { + $this->__set($this->updateTime, null); + } + // 事件回调 if (false === $this->trigger('before_write', $this)) { return false; @@ -295,6 +305,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if ($this->isUpdate) { // 自动更新 $this->autoCompleteData($this->update); + // 事件回调 if (false === $this->trigger('before_update', $this)) { return false; @@ -327,6 +338,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 自动写入 $this->autoCompleteData($this->insert); + // 自动写入创建时间 + if ($this->autoWriteTimestamp) { + $this->__set($this->createTime, null); + } + if (false === $this->trigger('before_insert', $this)) { return false; } @@ -411,11 +427,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $value = null; } if (!in_array($field, $this->change)) { - if (in_array($field, $this->autoTimeField)) { - $this->__set($field, $value); - } else { - $this->__set($field, isset($this->data[$field]) ? $this->data[$field] : $value); - } + $this->__set($field, isset($this->data[$field]) ? $this->data[$field] : $value); } } } @@ -916,7 +928,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 获取字段类型信息并缓存 $this->fieldType = self::db()->getTableInfo('', 'type'); } - if (is_null($value) && in_array($name, $this->autoTimeField)) { + if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime, $this->deleteTime])) { // 自动写入的时间戳字段 if (isset($this->type[$name])) { $type = $this->type[$name];