Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine merge: DateTime field always updated

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!

like image 272
T30 Avatar asked Feb 24 '16 13:02

T30


1 Answers

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

like image 64
Raymond A Avatar answered Oct 27 '22 02:10

Raymond A