模型的时间戳自动写入支持timestamp类型字段

This commit is contained in:
thinkphp
2016-05-12 21:53:54 +08:00
parent 67cebd8ca1
commit c3af0bb1c4
5 changed files with 29 additions and 1 deletions

View File

@@ -916,7 +916,23 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
{
if (is_null($value) && in_array($name, $this->autoTimeField)) {
// 自动写入的时间戳字段
$value = NOW_TIME;
if (isset($this->type[$name])) {
$type = $this->type[$name];
if (strpos($type, ':')) {
list($type, $param) = explode(':', $type, 2);
}
switch ($type) {
case 'timestamp':
$format = !empty($param) ? $param : $this->dateFormat;
$value = date($format, NOW_TIME);
break;
case 'datetime':
$value = NOW_TIME;
break;
}
} else {
$value = NOW_TIME;
}
} else {
// 检测修改器
$method = 'set' . Loader::parseName($name, 1) . 'Attr';
@@ -947,6 +963,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$value = strtotime($value);
}
break;
case 'timestamp':
$format = !empty($param) ? $param : $this->dateFormat;
$value = date($format, is_numeric($valiue) ? $value : strtotime($value));
break;
case 'object':
if (is_object($value)) {
$value = json_encode($value, JSON_FORCE_OBJECT);
@@ -1008,6 +1028,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$format = !empty($param) ? $param : $this->dateFormat;
$value = date($format, $value);
break;
case 'timestamp':
$format = !empty($param) ? $param : $this->dateFormat;
$value = date($format, strtotime($value));
break;
case 'json':
case 'array':
$value = json_decode($value, true);