[PR #869] Detach (DateTime) objects in original object data #8834

Closed
opened 2026-01-22 16:01:54 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/869

State: closed
Merged: No


The PR makes Doctrine ORM update the database when you execute something like this:

// MyEntity.php

/**
 * @Entity
 */
class MyEntity
{
    // ...

    /**
     * @Column(type="datetime")
     */
     private $created;

    // ...

    public function setCreated(\DateTime $created)
    {
        $this->created = $created;
    }

    public function getCreated()
    {
        return $this->created;
    }

    // ...
}
// Current problem

$entity = $entityManager->find('MyEntity', 1);

$entity->getCreated()->setDate('2001-01-01');

$entityManager->persist($entity);

$entityManager->flush();

// At this point no changes have been made to the database!

The reason this doesn't work currently is because the array of original data stored in the UnitOfWork contains the actual DateTime object which is in the entity. When you update the object in the entity the original data is updated also so the entity are the original value are still equal and therefore the object is not seen as changed.

I have chosen to clone all objects rather than just DateTime object include more types are included in the future.

**Original Pull Request:** https://github.com/doctrine/orm/pull/869 **State:** closed **Merged:** No --- The PR makes Doctrine ORM update the database when you execute something like this: ``` php // MyEntity.php /** * @Entity */ class MyEntity { // ... /** * @Column(type="datetime") */ private $created; // ... public function setCreated(\DateTime $created) { $this->created = $created; } public function getCreated() { return $this->created; } // ... } ``` ``` php // Current problem $entity = $entityManager->find('MyEntity', 1); $entity->getCreated()->setDate('2001-01-01'); $entityManager->persist($entity); $entityManager->flush(); // At this point no changes have been made to the database! ``` The reason this doesn't work currently is because the array of original data stored in the UnitOfWork contains the actual DateTime object which is in the entity. When you update the object in the entity the original data is updated also so the entity are the original value are still equal and therefore the object is not seen as changed. I have chosen to clone all objects rather than just DateTime object include more types are included in the future.
admin added the pull-request label 2026-01-22 16:01:54 +01:00
admin closed this issue 2026-01-22 16:01:54 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#8834