Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine modified DateTime is not persisted [duplicate]

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?

like image 535
blahblah Avatar asked Apr 06 '17 15:04

blahblah


3 Answers

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

like image 109
benlumley Avatar answered Oct 03 '22 02:10

benlumley


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

like image 20
Matteo Avatar answered Oct 03 '22 02:10

Matteo


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();
like image 38
Sofien Benrhouma Avatar answered Oct 03 '22 01:10

Sofien Benrhouma