踩的Thinkphp5 的自动时间戳的坑

1.A non well formed numeric value encountered

数据库表字段设置 datetime类型,返回的时候系统会自做聪明地对时间类型进行转换。

    protected function formatDateTime($time, $format, $timestamp = false)
    {
        if (false !== strpos($format, '\\')) {
            $time = new $format($time);
        } elseif (!$timestamp && false !== $format) {
            $time = date($format, $time);
        }
        return $time;
    }

 本身数据类型已经返回了datetime,再做一次转换就出现了“A non well formed numeric value encountered”错误。

解决方法:在database.php 中设置取消数据类型的自动转换:

'datetime_format' => false,

2.数据表字段不存在:[update_time]

如果启用了TP5里的自动时间戳,系统会默认当前模型有两个字段:create_time,update_time。

如果模型并不存在update_time,更新时就会出现该错误。

解决方法:

一:增加update_time,或者如果你的字段并不是默认的create_time和update_time,在模型中使用以下方法重定义:

protected $createTime = 'create_at';
protected $updateTime = 'update_at';

二:禁用自动时间戳:我就是不想要update_time。在database.php文件中定义:

'auto_timestamp'  => false,

猜你喜欢

转载自my.oschina.net/u/591185/blog/1633096