I have a unique
constraint on of the column. When this code runs, I get error log from framework but it is not what I have given in Exception block.
If unique column exists then I want to query its primary key and set it as $id
and return to the page. Right now it is stopping on db error and not going into Catch block
.
Here is my code:
try {
$result = $this->db->insert('email', $new_email);
if ($result)
{
$id = $this->db->insert_id();
} else {
throw new Exception("$$$$$$$$$$$$$Log database error");
}
} catch (Exception $e) {
log_message('error',$e->getMessage());
return;
}
**Error Messages**
I get from framework:
DEBUG - 2013-04-07 05:00:38 --> DB Transaction Failure
ERROR - 2013-04-07 05:00:38 --> Query error: Duplicate entry
I don't know what is wrong with it.
CI has no good support for exceptions. You DB queries will call some vague CI error_logging thingie called show_error(). What you need to do is setup proper exception handling.
Basically you can follow the entire recipe.
Now all your database errors will automatically throw exceptions. And as a bonus you have good exception handling in your entire CI application.
Register a custom errorhandler that transforms PHP errors into exceptions, for instance put this in top of your config/config.php
Register an uncaught exception handler, put something like this in your config/config.php
Set a termination handler:
Set a custom assert handler that converts asserts into exceptions, put something like this in your config/config.php:
Use wrappers like this in your controllers
You can tune and customize the whole thing to your likings!
Hope this helps.
You will also need to intercept the CI show_error method. Place this in application/core/MY_exceptions.php:
And leave in application/config/database.php this setting on FALSE to have database errors converted into exceptions.
CI has a few (very) weak points, such as exception-handling but this will go a long way correcting that.
If you are going to use transactions, make sure you do rollbacks on exceptions. Related to this NEVER (as in EVER) use persistant connections as open transactions and other session specific DB state will be picked up / continued by other sessions.