Symfony2: How to set default value for DateTime field

I am working with symfony 2.8

I have a table named: "Docentes" with the DateTime field "fechaAlta"

I need the default value of this field to be today when I add a new entry

I generate CRUD using the command generate:doctrine:crud

In Symfony's DocentesController.php file, create two functions: newAction and editAction (among others). Both using the same form , paste in the file "DocentesType.php" into the folder "Form":

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder

        ->add('fechaAlta', 'date')
        ->add('dni')
        #
    ;
}

I tried two solutions:

ONE: In the Entity file named "Docentes.php" I add a function:

  public function  __construct()
{
$this->fechaAlta = new \DateTime();    
}

, "fechaAlta" : 01, 01 2011. .

TWO:

buildForm:

->add('fechaAlta', 'date', array(
                'data' => new \DateTime()))
 ##

, , : 01, 01 2011, , Symfony , , 2016-03-25 ! , !

+4
2

:

public function  __construct()
{
$this->fechaAlta = new \DateTime();    
}

public function  __construct()
{
$this->fechaAlta = new \DateTime('now');    
}
0

:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('fechaAlta', 'date', array(
            'empty_data' => new \DateTime('now'),
        ))
        ->add('dni')
    ;
}
0

All Articles