交响乐 1.4 |如何为日期时间字段存储 NULL?
当我尝试为日期时间字段存储 NULL 时,它总是显示为 0000-00-00 00:00:00。我已经正确设置了架构和数据库,以便能够存储 NULL 值。 (允许存储“NULL”,甚至将这些日期时间字段的默认值设置为 NULL)。
我尝试过:
1.)“预绑定”方法(=>在actions.class中)
$values = $request->getParameter($form->getName());
$values['datetime_field'] = null;
$form->bind($values, $request->getFiles($form->getName()));
2.)“预保存”方法(=>在ctions.class中)
if ($form->isValid())
{
$form->getObject()->setDateTimeField(null);
$form->save();
3.)“ override updateObject()"-approach (=> 在 forms-class 中)
public function updateObject($values = null)
{
$object = parent::updateObject($values);
$object->setDateTimeField(null);
return $object;
}
一定有一些我没有想到的...为什么总是存储 0000-00-00 00:00:00 而不是 NULL?确实需要存储“任何内容”,因此 DBMS 本身可以处理“NULL 插入”...
任何帮助/提示都非常感谢! :)
When I try to store NULL for a DateTime-field it always comes out as 0000-00-00 00:00:00. I have set up the schema and the database correctly in order to be able to store NULL values. (allowed "NULL" to be stored and even set the default value for those datetime-fields to NULL).
I tried:
1.) The "pre-binding"-approach (=> in the actions.class)
$values = $request->getParameter($form->getName());
$values['datetime_field'] = null;
$form->bind($values, $request->getFiles($form->getName()));
2.) The "pre-saving"-approach (=> in the ctions.class)
if ($form->isValid())
{
$form->getObject()->setDateTimeField(null);
$form->save();
3.) The "override updateObject()"-approach (=> in the forms-class)
public function updateObject($values = null)
{
$object = parent::updateObject($values);
$object->setDateTimeField(null);
return $object;
}
There must be something I haven't thought of... why is there always 0000-00-00 00:00:00 stored instead of NULL? Really "nothing" needs to be stored, so the DBMS could handle the "NULL-insertion" itself...
Any help/hint is HIGHLY appreciated! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否 (a) 使用 Doctrine,并且 (b) 使用“datetime”作为模式中的字段类型?尝试切换到“时间戳” - 作为 数据类型部分说,这是存储日期和时间的字段的正确类型。它将映射到数据库中 MySQL 的
datetime
类型。Are you (a) using Doctrine, and (b) using "datetime" as the field type in your schema? Try switching to "timestamp" -- as the data types section of the Doctrine documentation says, this is the correct type for a field which stores both dates and times. It will map to MySQL's
datetime
type in the database.您确定在您的模式中,该字段有“notnull: false”吗?
Are you sure that in your schema, have you got "notnull: false" for this field?
还要检查您的 SQL 架构中,即在 phpmyadmin 中,默认列下的日期时间/时间戳字段是否为 NULL
Also check that in your SQL schema, i.e. in phpmyadmin, that the datetime/timestamp field is NULL under the Default column