2

When building a form in Symfony 3, I have some records in the database which are Datetime. Some are null, some are filled. I used Doctrine to generate the entities and such, the properties look like this:

/**
     * @var \DateTime
     *
     * @ORM\Column(name="date_start", type="datetime", nullable=false)
     */
    private $dateStart;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_end", type="datetime", nullable=true)
     */
    private $dateEnd;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_sale_start", type="datetime", nullable=true)
     */
    private $dateSaleStart;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_sale_end", type="datetime", nullable=true)
     */
    private $dateSaleEnd;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_insert", type="datetime", nullable=true)
     */
    private $dateInsert;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date_update", type="datetime", nullable=true)
     */
    private $dateUpdate;

There doesn't seem to be anything wrong as far as I can see. Then I build the form like so:

$repository = $this->getDoctrine()->getRepository('AppBundle:Offers');
$offer = $repository->find($id);
$form = $this->createFormBuilder($offer)
        ->add('date_end', DateTimeType::class, array(
            'empty_data' => '0000-00-00 00:00:00'
        ))
        ->add('date_sale_start', DateTimeType::class, array(
            'empty_data' => '0000-00-00 00:00:00'
        ))
        ->add('date_sale_end', DateTimeType::class, array(
            'empty_data' => '0000-00-00 00:00:00'
        ))
        ->add('date_insert', DateTimeType::class, array(
            'empty_data' => '0000-00-00 00:00:00'
        ))
        ->add('date_update', DateTimeType::class, array(
            'empty_data' => '0000-00-00 00:00:00'
        ))

This presents me with the following data object when I do dump($form):

-dateSaleStart: DateTime {#487 ▼
      +"date": "-0001-11-30 00:00:00.000000"
      +"timezone_type": 3
      +"timezone": "Europe/Amsterdam"
    }

I'm clueless as how to fix this. Who could help me out?

EDIT I also dump($offer) and in that object, there's also the wrong date. In the database it is a null field.

jbehrens94
  • 2,356
  • 6
  • 31
  • 59
  • You do the dump before or after the form is submitted? – goto Jul 25 '16 at 12:09
  • @goto Before I submit. It renders a HTTP 500 error before even rendering the view. – jbehrens94 Jul 25 '16 at 12:09
  • @Pieter that's cool and everything, but how do I actually *solve* it? – jbehrens94 Jul 25 '16 at 12:11
  • You can this result because of timezone settings. – Robert Jul 25 '16 at 12:13
  • 1
    You can solve it by not using an invalid date/time as your default value. – Pieter Jul 25 '16 at 12:13
  • @Robert and @Pieter I updated the `empty_data` setting to `2015-01-01 00:00:01`, but that still converts it to the negative year output. – jbehrens94 Jul 25 '16 at 12:22
  • I have not fooled around with the form date time class in some time but if you have nulls in your database then empty data should be null. Might need to tweak some other things to get it all working but null is null. – Cerad Jul 25 '16 at 12:42
  • So I'll never be able to render this form without replacing that null value with something else, or..? Can't I override something in the entity something? – jbehrens94 Jul 25 '16 at 12:49
  • Like: `getDate()` in the Entity. Check if null, if not return, if it is null...? – jbehrens94 Jul 25 '16 at 12:50
  • If for some reason you really want to replace null with some other date then a transformer is probably your best bet: http://symfony.com/doc/current/cookbook/form/data_transformers.html – Cerad Jul 25 '16 at 13:33

0 Answers0