2

I think I have found a bug which I can not find solution ..

I try to update the datetime field, but do not update it, don't gives me an error.

Move all other fields modifies them correctly, but the datetime field no.

$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyOwnBundle:Events')->find($id);
$In = $entity->getDateIn();
$In->modify('+1 day');
$entity->setDateIn($In);
$em->flush();

I also tried to insert a DateTime() object directly but does not update at all!

$entity->setDateIn(new \DateTime());

Is there a solution to this problem?

I installed symfony 2.1 and doctrine 2.3.3

EDIT Event entity:

/**
 * Events
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="My\OwnBundle\Entity\EventsRepository")
 */
 class Events
 {
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=100)
 */
private $name;

/**
 * @var string
 *
 * @ORM\Column(name="description", type="text")
 */
private $description;

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

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

....

/**
 * Set dateIn
 *
 * @param \DateTime $dateIn
 * @return Events
 */
public function setDateIn($dateIn)
{
    $this->dateIn = $dateIn;

    return $this;
}

/**
 * Get dateIn
 *
 * @return \DateTime 
 */
public function getDateIn()
{
    return $this->dateIn;
}

/**
 * Set dateOut
 *
 * @param \DateTime $dateOut
 * @return Events
 */
public function setDateOut($dateOut)
{
    $this->dateOut = $dateOut;

    return $this;
}

/**
 * Get dateOut
 *
 * @return \DateTime 
 */
public function getDateOut()
{
    return $this->dateOut;
}

....
Flexo
  • 87,323
  • 22
  • 191
  • 272

2 Answers2

7

The modify() method will not update the entity since Doctrine tracks DateTime objects by reference. You need to clone your existing DateTime object, giving it a new reference. Modify the new one and then set is as a new timestamp.

For more information, see the article in the Doctrine Documentation.

CarstenR
  • 71
  • 1
  • 4
  • Could you summarize the important points of the link? – C R Jan 25 '15 at 20:27
  • https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/cookbook/working-with-datetime.html#datetime-changes-are-detected-by-reference **DateTime changes are detected by Reference** When calling EntityManager#flush() Doctrine computes the changesets of all the currently managed entities and saves the differences to the database. In case of object properties (@Column(type=datetime) or @Column(type=object)) these comparisons are always made BY REFERENCE. That means the following change will NOT be saved into the database: – Arthur Bouchard Nov 29 '21 at 14:41
-1

the entity is right, but you need to persist your entity with $em->persist($entity) and you don't need to set again the date because the datetime is passed by reference

$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyOwnBundle:Events')->find($id);
$entity->getDateIn()->modify('+1 day');
$em->persist($entity);
$em->flush();
Marino Di Clemente
  • 3,120
  • 1
  • 23
  • 24