Symfony在MySQL数据库中设置DateTime [英] Symfony to set a DateTime in MySQL database

查看:34
本文介绍了Symfony在MySQL数据库中设置DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

在数据库Web应用程序中,日期/时间字段非常常见。

In a DB web app, Date/Time field is very common.

在Symfony中,您尝试过以下任一操作:

When in Symfony, you tried to do either of the follwings:

$creationDate=new DateTime();
$record->setCreationDate($creattionDate);

这将产生对象日期时间无法转换为字符串错误。

This will create a "Object DateTime can't be converted to string" error.

或:

$creationDate=new DateTime();
$datestr=$creationDate->format('Y-m-d H:i:s');
$record->setCreationDate($datestr);

这将在非对象上创建调用方法format()错误。

This will create "Calling method format() on a non-ojbect" error.

推荐答案

在您的实体中,您必须映射具有日期或日期时间类型的字段。

In your entity you have to map your field with date or datetime type.

@ ORM\ Column(name = creationDate,type = date,nullable = true)

@ORM\Column(name="creationDate", type="date", nullable=true)

这篇关于Symfony在MySQL数据库中设置DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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