This solution isn't atomic, so it's not safe in a concurrent
environment.  Some other client could insert a new record in between
your steps 1 and 3 and then your SQL statement would generate a
violation of the unique constraint even though you think it shouldn't.

You should use the database's enforcement of unique constraints.  Catch
the exception, but if you don't like the message it has, you can report
a different message to the user.

Regards,
Bill Karwin 

> -----Original Message-----
> From: Alex Netkachov [mailto:[EMAIL PROTECTED] 
> Sent: Friday, September 28, 2007 11:50 PM
> To: lauren49
> Cc: fw-general@lists.zend.com
> Subject: Re: [fw-general] Validation for multi-column unique 
> constraints
> 
> I prefer combination of the following methods:
> 1. Check the uniqueness using a SELECT query.
> 2. Display error
> 3. After fixing error, execute data modification statemenet.
> 4. In case of error I can do either of the following:
> 4.1. parse error message and display error or if it fails 
> 4.2. just show the general database error message and 
> optionally 4.3. show the general database error message with 
> recording exact message text to log
> 
> Sincerely,
> Alex
> 
> On 9/29/07, lauren49 <[EMAIL PROTECTED]> wrote:
> >
> > Hi,
> >
> > I understand that checking for a value's uniqueness against 
> a db table 
> > within in application is not a stable method and should not 
> be relied 
> > upon but the exception message thrown by a save attempt 
> when a value 
> > is not unique really doesn't make much sense to the user
> >
> > Caught exception: Zend_Db_Statement_Exception
> > Message: SQLSTATE[23000]: Integrity constraint violation: 1062 
> > Duplicate entry '12-Report 21' for key 2
> >
> > Instead I'd rather report a more user-friendly error message to the 
> > user such as
> >
> > 'Report 21' already exists
> >
> > I'm very curious to hear how other's may have handled this problem.
> >
> > Thank you,
> > Lauren
> >
> >
> > --
> > View this message in context: 
> > 
> http://www.nabble.com/Validation-for-multi-column-unique-constraints-t
> > f4537684s16154.html#a12951118 Sent from the Zend Framework mailing 
> > list archive at Nabble.com.
> >
> >
> 
> 
> --
> http://www.alexatnet.com/ - consulting, blog, articles and 
> support for PHP, ZF, JavaScript and web development.
> 

Reply via email to