debuggable

 
Contact Us
 

Handling database connection errors in CakePHP

Posted on 4/7/06 by Felix Geisendörfer

Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).

I often have the problem, that when I'm moving my CakePHP app's between different servers, I simply forget to upload the right app/config/database.php and the result is a huge mess of php errors reminding me of my stupidity ; ). So since clients don't like to see such errors and sometimes your database could really be unavailable for some reasons you cannot control, I thought it would be nice to display a well formated error message, stating the problem.

A quick search to the group brought up the general idea of checking the ConnectionManager's Datasource object and the rest was simply a matter of a couple lines of code. Here is what I came up with:

I changed my app/app_controller.php to look like this:

class AppController extends Controller
{
    function __construct()
    {
        parent::__construct();                
       
        $db =& ConnectionManager::getDataSource('default');
        if (empty($db->connection))
        {            
            $php_errors = @ob_get_clean();                    
           
            $this->viewPath = 'errors';
            $this->set('host', $db->config['host']);
            $this->set('user', $db->config['login']);
            $this->set('php_errors', $php_errors);
           
            $this->render('db_connection_failed', 'error');
            exit;
        }
    }
}

And added this little command to my app/config/bootstrap.php:

I think the code is pretty self explanatory. You only need to create a file app/views/errors/db_connection_failed.thtml and fill it with some information to make things work. If you wonder about the output buffering: If your DEBUG value (in app/config/core.php) is set higher then 0, CakePHP will display php errors because of the failed connection. My approach buffers them, so you can display them nicely formated in your error view if needed.

Note: This approach doesn't set the base/webroot/here etc. variable in your view, so if you run cakephp from within a subfolder your css includes might break. However, this could easily be added to the solution, it's just that I didn't need any fancy styling for my message.

See a screenshot of how I styled the error message.

 
&nsbp;

You can skip to the end and add a comment.

zupert  said on Oct 17, 2007:

very nice hack, but it doesn't function without putting the following line before the AppController class definition :

uses('model' . DS . 'connection_manager');

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.