views:

1722

answers:

1

I have a table with a unique key (date + userid) in my webapp database. When i try to insert record with existing date and userid, i'm getting error "dupicate key in table". I turn on database debug in application config, because i need use mysql error number and message. And now i need processing this error. I can use hard coded constants in this controller, but i think it's not good idea - i need database error handling in many places and i dont like standate CI error handling.

How are you processing database errors? What is the best-practice?

+4  A: 

We use in our project constructions like these:

$this->db->_error_number();
$this->db->_error_message();

but this undocumented functions and in next releases this could change. Of course you can simply use standard php for mysql error handle function:

mysql_errno()
mysql_error()

internally CI use these functions.

As for me the best practice is use in custom Model's base class (BaseModel) $this->db->_error_number(); and determine error, next throw exception with information error message taken from $this->db->_error_message(); All your Model derived from BaseModel, and call method to check last request for db error and your Model must handle exception, and process it (may be additionally log), of course you can implement check result as result value and avoid of throwing exception.

pocheptsov
i user this functions too. BaseModel it`s good idea, and i think about it. Thanks.IMO throw exception method with model handling exception is bit difficult and no transparent. I need simple processing errors immediately after query run. Than i need to make decission rely on _error_number()
drnk
Those aren't constructors, but good answer.
Zack