How catch SQL Exception?

Hi All,
How Can I catch SQL error "Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry"
in Cake style?
For my app this error is a normal, but it gets me each time new record in log file.

Thank you.

You might need to show the Controller or Model code that is causing this error, and perhaps the table definition.

I’ve not actually used the new ORM yet, but missing a $model->create() or trying to set the value on a column with a unique or primary index when it already exists might trigger something like this. Or perhaps the table is missing an auto_increment on the primary key.

Hi,
As I told it’s normal behavior, it happens because partner’s postback sends data twice and transaction_id is unique.
I’d like catch this error and suppress without records in error log.

The difficult thing is that SQL typically doesn’t throw exceptions, and if
there is one, it’s throw by cakephp and isn’t specific to a create issue.
Even if you can catch the error, the ORM will always log any SQL error.

If it’s a known issue, it might be better to do a find on the unique
transaction ID, and if the record already exists, drop the rest of the
processing.

Easiest way to set validation rule isUnique, and Cake does it, bu it is additional server load

You can catch the \PDOException. Here is an example code that does exactly that:

Спасибо за ваш ответ.

Thanks for answer, it’s interesting, but I though it’s possible with CakeException class.