7

I try to modify DateTime field of the object with modify function

    $em = $this->getDoctrine()->getManager();
    $end = $session->getEndDate();
    $session->setEndDate($end->modify('+10 seconds'));
    $em->persist($session);
    $em->flush();

This is setter for $endDate field in Session class:

  /**
    * @param \DateTime $endDate
    */
   public function setEndDate(\DateTime $endDate)
   {
       $this->endDate = $endDate;
   }

Why end date changes can't be persisted to database?

blahblah
  • 1,010
  • 15
  • 40

4 Answers4

7

Doctrine won't save changes to the existing DateTime instance (due to the internals of PHP's equality testing I think)

If you clone the object and then set it back, should work. Or clone it in the setter?

See Doctrine2 ORM does not save changes to a DateTime field

Community
  • 1
  • 1
benlumley
  • 11,370
  • 2
  • 40
  • 39
3

You need to flush it:

$em->flush($session);

The persist is only for entity not yet created.

UPDATE:

The modify method don't return anything, affect the specify object instance, so you can simply try:

$end = $session->getEndDate();
$end->modify('+10 seconds');
$em->flush();

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115
1

You need to add merge or flush to save the update

 $end = $session->getEndDate();
 $session->setEndDate($end->modify('+10 seconds'));
 $em->persist($session);
 $em->flush();
0

Based on @Cerad 's comments, I wonder if this will work for you:

$em = $this->getDoctrine()->getManager();
$end = new \DateTime( $session->getEndDate() );
$end->modify('+10 seconds');
$session->setEndDate( $end );
$em->persist($session);
$em->flush();

Can you try it out. I'm not certain it will make a difference though.

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45