[DDC-1716] Better unique constraints handling or even updateIfExists/findOneOrCreate Created: 19/Mar/12  Updated: 19/Apr/12

Status: Open
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: 2.2.1
Fix Version/s: None
Security Level: All

Type: New Feature Priority: Minor
Reporter: Konstantin Assignee: Benjamin Eberlei
Resolution: Unresolved Votes: 6
Labels: None


It is too hard handle rows duplication now. As adviced here http://stackoverflow.com/questions/3967226/checking-for-duplicate-keys-with-doctrine-2 we should catch \PDOException with code 23000. This is bad behavior.

So I propose:
a. Add DuplicateRowException wich will throwed by flush method (maybe wrapped PDOException) with method "getDuplicatedProperties" (based on sql error description parsing + mapping)
b. Add method findOneOrCreate (hello, Propel!) or updateIfExists to ObjectInterface, EntityManager
c. Combine a, b

Comment by Mark van der Velden [ 19/Apr/12 ]

I don't think it should be limited to the flush method, however, as this can occur with "custom" queries also. It would be ideal if the errors being generated can be much more easily (read: standardized) caught or read.

I'm in favor of letting the database handle integrity checks, and not run a query to decide wether or not the update or insert queries will violate. Hence the desire to handle this more gracefully.

Comment by Lucas van Lierop [ 19/Apr/12 ]

The DBAL statement class (https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Statement.php) has and execute method which might be a good place to catch and convert the generic PDOExceptions by wrapping the line

$stmt = $this->stmt->execute($params);

in a try/catch statement which then calls some kind of PDOException to Doctrine Exception method

Generated at Sat Sep 05 12:33:51 EDT 2015 using JIRA 6.4.10#64025-sha1:5b8b74079161cd76a20ab66dda52747ee6701bd6.