From 708dd2669f263bf2a3f03d15c0a5d21b4b43002e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 8 Apr 2016 11:32:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4Moel=E7=B1=BB=20createTimeFie?= =?UTF-8?q?ld=20updateTimeField=20timestamps=E5=B1=9E=E6=80=A7=20=E6=94=B9?= =?UTF-8?q?=E7=94=B1=E5=AE=9A=E4=B9=89timestampField=E5=B1=9E=E6=80=A7=20?= =?UTF-8?q?=E5=B9=B6=E4=B8=94=E9=85=8D=E5=90=88=20insert=E5=92=8Cupdate?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 93 +++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 59 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 7e9fa9fe..2fbf1113 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -49,14 +49,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 字段完成规则 protected $auto = []; - // 自动时间戳记录 - protected $timestamps = true; - // 新增时间戳字段 - protected $createTimeField = 'create_time'; - // 更新时间戳字段 - protected $updateTimeField = 'update_time'; // 时间戳字段列表 - protected $timestampField = []; + protected $timestampField = ['create_time', 'update_time']; // 新增要自动完成的字段列表 protected $insert = []; @@ -77,9 +71,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function __construct($data = []) { - if (empty($data)) { - $this->isUpdate = false; - } elseif (is_object($data)) { + $this->isUpdate = false; + if (is_object($data)) { $this->data = get_object_vars($data); } else { $this->data = $data; @@ -156,37 +149,43 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function __set($name, $value) { - // 检测修改器 - $method = 'set' . Loader::parseName($name, 1) . 'Attr'; - if (method_exists($this, $method)) { - $value = $this->$method($value, $this->data); - } + if (is_null($value) && in_array($name, $this->timestampField)) { + // 如果是时间戳字段 则自动写入 + $value = NOW_TIME; + } else { + // 检测修改器 + $method = 'set' . Loader::parseName($name, 1) . 'Attr'; + if (method_exists($this, $method)) { + $value = $this->$method($value, $this->data); + } - // 类型转换 或者 字符串处理 - if (isset($this->type[$name])) { - $type = $this->type[$name]; - switch ($type) { - case 'integer': - $value = (int) $value; - break; - case 'float': - $value = (float) $value; - break; - case 'boolean': - $value = (bool) $value; - break; - case 'array': - if (is_array($value)) { - $value = json_encode($value, JSON_UNESCAPED_UNICODE); - } - break; + // 类型转换 或者 字符串处理 + if (isset($this->type[$name])) { + $type = $this->type[$name]; + switch ($type) { + case 'integer': + $value = (int) $value; + break; + case 'float': + $value = (float) $value; + break; + case 'boolean': + $value = (bool) $value; + break; + case 'array': + if (is_array($value)) { + $value = json_encode($value, JSON_UNESCAPED_UNICODE); + } + break; + } } } - // 设置数据对象属性 + // 标记字段更改 if (isset($this->data[$name]) && $this->data[$name] != $value && !in_array($name, $this->change)) { $this->change[] = $name; } + // 设置数据对象属性 $this->data[$name] = $value; } @@ -327,11 +326,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } } - if ($this->timestamps) { - // 自动更新时间戳 - $this->data[$this->updateTimeField] = NOW_TIME; - } - // 检测是否为更新数据 if ($this->isUpdate()) { @@ -368,11 +362,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return false; } - if ($this->timestamps) { - // 自动写入时间戳 - $this->data[$this->createTimeField] = NOW_TIME; - } - // 自动写入 foreach ($this->insert as $field) { if (!in_array($field, $this->change)) { @@ -591,25 +580,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * 查找多条记录 * @access public * @param mixed $data 主键列表 - * @param false|string|array $load 预载入模型 * @return array|string */ - public static function all($data = [], $load = false) + public static function all($data = []) { $resultSet = self::db()->select($data); - if ($load) { - // 预载入关联模型 - if (is_string($load)) { - $load = (array) $load; - } - - foreach ($resultSet as &$result) { - foreach ($load as $relation) { - $model = new static($result); - $result->$relation = $model->$relation(); - } - } - } return $resultSet; }