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 are changes and generates the correct update query.
$entity = new Entity();
$entity->setId(1);
$entity->setName('test');
$EntityManager->merge($entity);
$EntityManager->flush();
Suppose that the element with the id=1 already exists in the db: if the name is different from 'test', Doctrine generates this query:
UPDATE table SET name = ? WHERE id = ? ["test","1"]
If I run again the code above, Doctrine recognizes that nothing is changed, and no query is committed.
But... when I set a DateTime field, Doctrine consider it as changed and always runs the update query:
$entity = new Entity();
$entity->setId(1);
$entity->setDate(new \DateTime(2000-01-01));
$EntityManager->merge($entity);
$EntityManager->flush();
//* ALWAYS commits this query:
>> UPDATE table SET date = ? WHERE id = ? ["2000-01-01 00:00:00","1"]
Do you know a way to avoid this useless update? Thanks!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With