删除Moel类 createTimeField updateTimeField timestamps属性 改由定义timestampField属性 并且配合 insert和update属性

This commit is contained in:
thinkphp
2016-04-08 11:32:42 +08:00
parent 7112389ca7
commit 708dd2669f

View File

@@ -49,14 +49,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 字段完成规则 // 字段完成规则
protected $auto = []; protected $auto = [];
// 自动时间戳记录
protected $timestamps = true;
// 新增时间戳字段
protected $createTimeField = 'create_time';
// 更新时间戳字段
protected $updateTimeField = 'update_time';
// 时间戳字段列表 // 时间戳字段列表
protected $timestampField = []; protected $timestampField = ['create_time', 'update_time'];
// 新增要自动完成的字段列表 // 新增要自动完成的字段列表
protected $insert = []; protected $insert = [];
@@ -77,9 +71,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
public function __construct($data = []) public function __construct($data = [])
{ {
if (empty($data)) { $this->isUpdate = false;
$this->isUpdate = false; if (is_object($data)) {
} elseif (is_object($data)) {
$this->data = get_object_vars($data); $this->data = get_object_vars($data);
} else { } else {
$this->data = $data; $this->data = $data;
@@ -156,37 +149,43 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
public function __set($name, $value) public function __set($name, $value)
{ {
// 检测修改器 if (is_null($value) && in_array($name, $this->timestampField)) {
$method = 'set' . Loader::parseName($name, 1) . 'Attr'; // 如果是时间戳字段 则自动写入
if (method_exists($this, $method)) { $value = NOW_TIME;
$value = $this->$method($value, $this->data); } else {
} // 检测修改器
$method = 'set' . Loader::parseName($name, 1) . 'Attr';
if (method_exists($this, $method)) {
$value = $this->$method($value, $this->data);
}
// 类型转换 或者 字符串处理 // 类型转换 或者 字符串处理
if (isset($this->type[$name])) { if (isset($this->type[$name])) {
$type = $this->type[$name]; $type = $this->type[$name];
switch ($type) { switch ($type) {
case 'integer': case 'integer':
$value = (int) $value; $value = (int) $value;
break; break;
case 'float': case 'float':
$value = (float) $value; $value = (float) $value;
break; break;
case 'boolean': case 'boolean':
$value = (bool) $value; $value = (bool) $value;
break; break;
case 'array': case 'array':
if (is_array($value)) { if (is_array($value)) {
$value = json_encode($value, JSON_UNESCAPED_UNICODE); $value = json_encode($value, JSON_UNESCAPED_UNICODE);
} }
break; break;
}
} }
} }
// 设置数据对象属性 // 标记字段更改
if (isset($this->data[$name]) && $this->data[$name] != $value && !in_array($name, $this->change)) { if (isset($this->data[$name]) && $this->data[$name] != $value && !in_array($name, $this->change)) {
$this->change[] = $name; $this->change[] = $name;
} }
// 设置数据对象属性
$this->data[$name] = $value; $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()) { if ($this->isUpdate()) {
@@ -368,11 +362,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
return false; return false;
} }
if ($this->timestamps) {
// 自动写入时间戳
$this->data[$this->createTimeField] = NOW_TIME;
}
// 自动写入 // 自动写入
foreach ($this->insert as $field) { foreach ($this->insert as $field) {
if (!in_array($field, $this->change)) { if (!in_array($field, $this->change)) {
@@ -591,25 +580,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
* 查找多条记录 * 查找多条记录
* @access public * @access public
* @param mixed $data 主键列表 * @param mixed $data 主键列表
* @param false|string|array $load 预载入模型
* @return array|string * @return array|string
*/ */
public static function all($data = [], $load = false) public static function all($data = [])
{ {
$resultSet = self::db()->select($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; return $resultSet;
} }