1
votes

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! :)

3
Can we see your schema? Are you storing this in a "timestamp" type or a "date" type?Matt Gibson

3 Answers

1
votes

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.

0
votes

Are you sure that in your schema, have you got "notnull: false" for this field?

0
votes

Also check that in your SQL schema, i.e. in phpmyadmin, that the datetime/timestamp field is NULL under the Default column

date_time_field datetime DEFAULT NULL,