Modified DateTime doctrine is not saved

I am trying to change the DateTime field of an object using the function modify

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

      

This is the setter for the $ endDate field in the Session class:

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

      

Why can't end date changes persist to the database?

+3


source to share


4 answers


Doctrine will not persist changes to an existing DateTime instance (due to internal PHP equality testing phenomena, I think)

If you clone an object and then set it back, it should work. Or clone it into a setter?



See Doctrine2 ORM does not save changes to DateTime field

+3


source


You need to clear it:

$em->flush($session);

      

Saving is only saved for an object that has not yet been created.

UPDATE:



The method modify

returns nothing, affects the instance of the object object, so you can just try:

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

      

Hope for this help

+5


source


You need to add a merge or flash to save the update

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

      

+1


source


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();

      

You may try. I'm not sure if this will make a difference.

0


source







All Articles