I am trying to implement "IF record EXISTS THEN UPDATE ELSE INSERT" in mysql. Specifically, I have to figure out how to do that with Doctrine ORM.
One solution using native MySql is to use "ON DUPLICATE KEY UPDATE". This is unfortunately not supported in Doctrine.
Another semi solution is to use "REPLACE INTO" syntax. Doctrine supports this, however REPLACE doesn't UPDATE records, instead it drops the record and creates a new one (with a new auto increment id.) Because of this, it's a bad solution if you depend on auto-increment ids as I do.
The table I'm trying to operate on is INNODB, so I have looked into transactions, but I don't fully understand if the same rules apply inside of transactions as apply outside of them.
Can I say "IF(INSERT IGNORE INTO table critical_id
= $id) THEN exit ELSE (UPDATE table SET field
= 'new_value')" inside of a transaction?
If I can't say something like that, is there any solution to this problem?