1

I had had a problem to set a Form Field DateTime (named endDate) to the Form Field DateTime (named startDate + 24h) if no data for endDate is typed into the form) The solution (thanks to the answers) was this:

 $em = $this->getDoctrine()->getManager();
            $task->setEndDate($form->get('startDate')->getData());
            $task->getEndDate()->modify('+1 day');
            $em->persist($task);
            $em->flush();

But when i modify the endDate + 1 day , the startDate is modified , too.

What's the best way to fix this?

Community
  • 1
  • 1
ChrisS
  • 736
  • 2
  • 8
  • 21

1 Answers1

0

Objects are always passed by reference in PHP, and so are instances of \DateTime. To fix this, clone the object instead of passing the same reference:

$task->setEndDate(clone $form->get('startDate')->getData());
Gerry
  • 6,012
  • 21
  • 33
  • Thanks!! This is what i was looking for: $task->setEndDate(clone $form->get('startDate')->getData()); – ChrisS Mar 29 '13 at 14:05