0

First of all thank you for reading and trying to help me. I'm new in symfony.

I have an entity with property FechaAlta (SingUpDate). I want to save the user sing up date

/**
 * @var date
 *
 * @ORM\Column(name="fechaAlta", type="datetime")
 */
private $fechaAlta;

/**
 * Set fechaAlta
 *
 * @return Promotor
 */
public function setFechaAlta()
{
    $this->fechaAlta = new \DateTime('now');

    return $this;
}

I'd like to know what is the best way to save this date without having a hidden field on the form.

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('nombre')
        #->add('slug')
        ->add('fechaAlta')
    ;
}

I have tried removing the form field "fechaAlta" but keep get the following error

An exception occurred while executing 'INSERT INTO Promotor (nombre, slug, fechaAlta) VALUES (?, ?, ?)' with params {"1":"Prueba","2":"prueba","3":null}:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'fechaAlta' cannot be null

In the newAction() i call $promotor->setFechaAlta(); that should save the current date.

public function newAction()
{
    $promotor = new Promotor();

    $promotor->setFechaAlta();

    $form   = $this->createForm(new PromotorType(), $promotor);

    return $this->render('PromotorBundle:Promotor:new.html.twig', array(
        'entity' => $promotor,
        'form'   => $form->createView(),
    ));
}

Thank you so much

JGrinon
  • 1,453
  • 1
  • 14
  • 36
  • The error message tells you exactly what is wrong – John Conde Apr 22 '13 at 17:33
  • Hi, welcome to StackOverflow! I have marked this as a possible duplicate of [Default value in Doctrine](http://stackoverflow.com/questions/3376881/default-value-in-doctrine) – kgilden Apr 22 '13 at 17:33
  • Thank you!! this post has helped me to find the solution, although it has been in other subject. http://stackoverflow.com/questions/9526272/default-column-value-with-doctrine2-and-symfony2-using-yaml I don't know if this will be the best way but it works. I have delete de field "fechaAlta" and set the sing up date in __construct() `public function __construct() { $this->fechaAlta = new \DateTime('now'); # Valor por defecto para fecha }` thanks! – JGrinon Apr 22 '13 at 17:55

2 Answers2

2

You can use @Lighthart solution or you can use your entity constructor.
The advantage of using the entity constructor is that if you create this type of object in a second controller/action and want to keep the same behavior, you don't need to repeat yourself.

//Your entity code
/**
 * @var date
 *
 * @ORM\Column(name="fechaAlta", type="datetime")
 */
private $fechaAlta;

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

Don't forget to remove the field from your form:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('nombre');
}
cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
0

The default symfony pattern is that the newAction makes a form that posts to the createAction.

createAction is where you should set your date:

public function createAction( Request $request, $id ) {
    $promotor = new Promotor();
    $promotor->setFechaAlta();

    $form   = $this->createForm(new PromotorType(), $promotor);
    $form->bind( $request );

    if ( $form->isValid() ) {
        $this->getDoctrine()->getManager()->persist( $promotor );
        $this->getDoctrine()->getManager()->flush();

    return $this->redirect( 
        $this->generateUrl( 'promotor_show', array( 'id' => $promotor->getId() ) ) 
        );
    }

    return $this->render( 'PromotorBundle:Promotor:new.html.twig'
        , array(
        )
    );
}
Lighthart
  • 3,648
  • 6
  • 28
  • 47