Show datetime in symfony form but store unixtime

Viewed 25

I'have this fields on my enity to store two dates in unixtime format

 /**
     * @var \int
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="activation_date", type="integer", nullable=true)
     */
    private $activationDate;

    /**
     * @var \int
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(name="deactivation_date", type="integer", nullable=true)
     */
    private $deactivationDate;

but in the formtype class are those fields are setted as Datetime in order to show a date field to the user

->add('activation_date', DateTimeType::class, [
                'label_attr' => [
                    'class' => 'font-weight-bold'
                ],
                'required' => false,
            ])
            ->add('deactivation_date', DateTimeType::class, [
                'label_attr' => [
                    'class' => 'font-weight-bold'
                ],
                'required' => false,
            ])

and the fields are shown in the view this way

 {{ form_start(form) }}
 {{ form_row(form.title) }}
 {{ form_row(form.description) }}
 {{ form_row(form.activation_date) }}
 {{ form_row(form.deactivation_date) }}
 {{ form_row(form.active) }}

this code not work because the entity receives datetime and expects int values, how can I store unixtime values on the database but show datetime fields in the form for create and edit??

1 Answers

Set field option input to timestamp. Like this:

->add('activation_date', DateTimeType::class, [
    'label_attr' => [
        'class' => 'font-weight-bold'
    ],
    'required' => false,
    'input' => 'timestamp'
])
->add('deactivation_date', DateTimeType::class, [
    'label_attr' => [
        'class' => 'font-weight-bold'
    ],
    'required' => false,
    'input' => 'timestamp'
])
Related