原则:将CURRENT_TIMESTAMP设置为数据库的默认值(即不是由PHP) [英] Doctrine: Set CURRENT_TIMESTAMP as default value by the database (i.e. not by PHP)

查看:174
本文介绍了原则:将CURRENT_TIMESTAMP设置为数据库的默认值(即不是由PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看周围一段时间后,我仍然找不到一种方法来获取数据库服务器插入的CURRENT_TIMESTAMP(默认值为 INSERT )。



问题:当您将对象保留到数据库时,Doctrine将缺少的字段显式设置为NULL。所以看起来,在表定义中设置默认值,根本没有任何影响: - (



我不想设置时间PHP(例如 $ object-> setTimestamp(new \DateTime()); ),因为这可能会返回与数据库服务器具有的不同的时间,如下所述: https://stackoverflow.com/a/3705090/1668200



我迄今为止所尝试的:





我发现的任何其他解决方案(包括Doctrine扩展'Timestampable' https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/timestampable.md )使用PHP的时间。

解决方案看看这个: https://stackoverflow.com/a/29384596/3255540 它应该解决问题与错误:调用成员函数格式()在字符串并强制SQL NOW()要发送到数据库。


After looking around for a while, I still couldn't find a way to get CURRENT_TIMESTAMP inserted by the database server (as default value on INSERT).

The problem: When you persist an object to the database, missing fields are explicitly set to NULL by Doctrine. So it looks like, setting a default value in the table definition, doesn't have any effect at all :-(

I don't want to set the time through PHP (e.g. $object->setTimestamp(new \DateTime());) cause this might return a different time than what the database server has, as explained here: https://stackoverflow.com/a/3705090/1668200

What I've tried so far:

Any other solution I found (including the Doctrine extension 'Timestampable' https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/timestampable.md ) uses PHP's time.

解决方案

Have a look at this: https://stackoverflow.com/a/29384596/3255540 it should resolve the problem with Error: Call to a member function format() on string and force SQL NOW() to be sent to the database.

这篇关于原则:将CURRENT_TIMESTAMP设置为数据库的默认值(即不是由PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆