问题描述
查看周围一段时间后,我仍然找不到一种方法来获取数据库服务器插入的CURRENT_TIMESTAMP(默认值为 INSERT
)。
问题:当您将对象保留到数据库时,Doctrine将缺少的字段显式设置为NULL。所以看起来,在表定义中设置默认值,根本没有任何影响: - (
我不想设置时间PHP(例如 $ object-> setTimestamp(new \DateTime());
),因为这可能会返回与数据库服务器具有的不同的时间,如下所述:
我迄今为止所尝试的:
-
发送
NOW
例如$ object-> setTimestamp('NOW()');
),如下所述:
=>没有工作:错误:调用一个成员字符串上的函数format()
-
在持久化之前从对象中删除timestamp属性(请参阅)也没有工作:无论如何,Doctrine将该字段设置为NULL。
我发现的任何其他解决方案(包括Doctrine扩展'Timestampable')使用PHP的时间。
错误:调用成员函数格式()在字符串
并强制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:
Send in
NOW
literally (e.g.$object->setTimestamp('NOW()');
), as explained here: https://stackoverflow.com/a/13850741/1668200
=> Didn't work:Error: Call to a member function format() on string
Removing the 'timestamp' property from the object just before persisting it (see https://stackoverflow.com/a/3600758/1668200 ) didn't work either: The field was set to NULL by Doctrine anyway.
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!