From a39814d6950b5f10e55001435806c565c44b04a8 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 7 Apr 2016 18:40:56 +0800 Subject: [PATCH] =?UTF-8?q?Model=E7=B1=BB=E5=A2=9E=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=AE=B0=E5=BD=95=E6=97=B6=E9=97=B4=E6=88=B3=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=8A=9F=E8=83=BD=20=E6=94=B9=E8=BF=9B=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=87=AA=E5=8A=A8=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 59 +++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 9ede8043..d9354881 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -48,14 +48,18 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $validate; // 字段完成规则 protected $auto = []; + + // 自动时间戳记录 + protected $timestamps = true; + // 新增时间戳字段 + protected $createTimeField = 'create_time'; + // 更新时间戳字段 + protected $updateTimeField = 'update_time'; + // 新增的字段完成 - protected $insert = [ - 'create_time' => 'time', - ]; + protected $insert = []; // 更新的字段完成 - protected $update = [ - 'update_time' => 'time', - ]; + protected $update = []; // 字段类型或者格式转换 protected $type = []; @@ -169,10 +173,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess case 'boolean': $value = (bool) $value; break; - default: - if (is_callable($type)) { - $value = call_user_func_array($type, [$value]); - } } } @@ -207,10 +207,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess case 'boolean': $value = (bool) $value; break; - default: - if (is_callable($type)) { - $value = call_user_func_array($type, [$value]); - } } } @@ -318,10 +314,13 @@ 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); } - $data[$name] = is_callable($rule) ? call_user_func_array($rule, [ & $data]) : $rule; } + if ($this->timestamps) { + $data[$this->updateTimeField] = NOW_TIME; + } // 检测是否为更新数据 if ($this->isUpdate($data)) { @@ -337,7 +336,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 自动更新 foreach ($this->update as $name => $rule) { - $data[$name] = is_callable($rule) ? call_user_func_array($rule, [ & $data]) : $rule; + $data[$name] = $this->auto($name, $rule, $data); } $db = self::db(); @@ -357,9 +356,13 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return false; } + if ($this->timestamps) { + $data[$this->createTimeField] = NOW_TIME; + } + // 自动写入 foreach ($this->insert as $name => $rule) { - $data[$name] = is_callable($rule) ? call_user_func_array($rule, [ & $data]) : $rule; + $data[$name] = $this->auto($name, $rule, $data); } $result = self::db()->insert($data); @@ -385,6 +388,28 @@ 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