Check cakephp database configuration exist
Check cakephp database can be connect
In cakephp you need to configure the connection
The configuration file was in app/config/database.php
Inside the file, you can see 2 variable which is 'default' and 'test'
By default is 'default' but if you want to use 'test' in the controller for example, you need to call it like this
$this->Mmodel->useDbConfig = 'test';
Problem arise is, can we connect to 'test' without problem?
So, here is the code I made. Been googling it and quite hard to found the solution, thus I'm putting this in my blog. lol...
This code will check
- Does the configuration 'test' exist
- If the configuration exist, does the database connection valid
If these 2 condition is pass, you can only have SQL Syntax Error like selecting table that does not exist
So here is the code (its free)
//Checking is done by connecting with the database if (class_exists('DATABASE_CONFIG')){ $dbConfigName1 = 'test'; //<-- PUT YOUR DATABASE CONFIG VARIABLE NAME HERE $dbConfig1 = new DATABASE_CONFIG(); //pr( $dbConfig1); if( isset( $dbConfig1->$dbConfigName1)){ //Now check wether the database configuration, its alive! //pr( $dbConfig1->$dbConfigName1); $dbTemp1 =& ConnectionManager::getDataSource($dbConfigName1); if( $dbTemp1->isConnected() == true){ //Means database exist and you can use it echo "DATABASE CONFIG '$dbConfigName1' EXIST AND MANAGE TO CONNECT"; } else{ //Database configuration exist, but cannot connect to the database echo "DATABASE CONFIG '$dbConfigName1' EXIST BUT FAILED TO CONNECT"; } } else{ //The database configuration does not exist echo "HAS NO DATABASE CONFIGURATION WITH NAME '$dbConfigName1'"; } } else{ //The database.php file not even exist. How can this be? IMPOSIBIEBERBEL! echo "CANNOT FIND THE 'database.php' CONFIGURATION FILE"; }
Reference
http://bakery.cakephp.org/articles/T0aD/2009/07/08/handle-database-connection-errors
http://debuggable.com/posts/handling-database-connection-errors-in-cakephp:480f4dd5-9570-421a-a04d-43cdcbdd56cb