There may be times where Doctrine may or may not know if an entity already exists in your database. Doctrine ORM provides a handy way to merge an entity to the database, meaning that it will insert any new entities, and update any existing ones.
Most PHP developers come from a MySQL background, so you’re probably well-aware of you you can use ‘ON DUPLICATE KEY’ to achieve this:
INSERT INTO table (id, value) values('test', 'TEST') ON DUPLICATE KEY UPDATE value = values(name)
/*
See more at: http://vivait.co.uk/blog/updating-entities-when-insert-has-duplicate-key-doctrine-orm1/
*/
Unfortunately, because Doctrine is database agnostic, this will not work when using DQL. Instead, Doctrine ORM provides a handy way of achieving this within the ORM:
$entity = new Table();
$entity->setId(1);
$entity->setValue('TEST');
$entityManager->merge($entity);
$entityManager->flush();
/**
- See more at: http://vivait.co.uk/blog/updating-entities-when-insert-has-duplicate-key-doctrine-orm1/
**/