I create a new Entity with an existing Id
, and I want to update the related database record.
Doctrine merge has been my best friend: recognizes if there
As said by @Raymond, it's the expected behavior.
It's a pity, but if your merge doesn't depend on the date
property, a workaround could be to set the date after the merge like this:
$entity = new Entity();
$entity->setId(1);
// Set all fields that the merge is depending on
// ...
$EntityManager->merge($entity);
$entity->setDate(new \DateTime('2000-01-01'));
$EntityManager->flush();
Update
After tried, the only alternative seems to retrieve the merged object and update it by flushing the EntityManager again.
You can use:
$entity = new Entity();
$entity->setId(1);
// Set all fields that the merge is depending on
$EntityManager->merge($entity);
$EntityManager->flush();
$entity = $EntityManager->getRepository('Your\Entity')->find(1); // Retrieve the entity
$entity->setDate(new \DateTime('2000-01-01'));
$EntityManager->flush(); // Reflush
Update2
The cleaner way I found to achieve the update after the merge is re-merge the entity, e.g. :
$entity = new Entity();
$entity->setId(1);
// Set all fields that the merge is depending on
$EntityManager->merge($entity); // The real merge that retrieve (without commit) or create
$EntityManager->flush();
$entity->setDate(new \DateTime('2000-01-01'));
$entityManager->merge($entity); // Remerge the object with the changed field
$EntityManager->flush(); // Working re-flush
But this doesn't change the main problem and doesn't really make sense because you cannot compare the DateTime
object by yourself, $entity->getDate()
return always null before calling setDate
, even after the first merge.
Doctrine compare the objects by reference (hash) with ===
, also a new instance of \DateTime
causes the update even if the object date is unchanged.
This is a really problematic issue that could be fixed by using ==
as comparison operator, but doctrine can't make a specific condition for \DateTime
without breaking their generic object comparison mechanism, that involves to decrease performances of one of the most used feature.
Apparently it's seems to be a bug in Doctrine which is still not resolved(Reference Github)
That is expected behavior, objects are compared by reference