0

Currently I am using dynamic database switching in Cakephp from the database config file. I am switching based on the subdomain ie: TEST.mysite.com and PROD.mysite.com.

How and where is the best place to test and redirect if there is a database connection?

Thanks,

kSeudo

1
  • Which version of cakephp (1.x or 2.x) and can you please post an example of your config. Commented Mar 20, 2012 at 11:53

1 Answer 1

2

The quick and dirty way to do this is to put a condition in the constructor. I say dirty because it creates a conditional config and with the introduction of a bug could leave your production app connected to a dev database.

class DATABASE_CONFIG {

    public $default = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => '',
        'login' => '',
        'password' => '',
        'database' => '',
        'prefix' => '',
        'encoding' => 'utf8'
    );

    public $testDB = array(
        'driver' => 'mysql',
        'persistent' => false,
        'host' => '',
        'login' => '',
        'password' => '',
        'database' => '',
        'prefix' => '',
        'encoding' => 'utf8'
    );

    public function __construct()
    {
        if (false !== stripos($_SERVER['HTTP_HOST'], 'test'))
        { // Use the test DB since 'test' is present in the server host
            $this->default = $testDB;
        }
    }
}

To test connection you could put the following in the contructor for app_model.php

public function __construct()
{
    parent::__construct();                

    $db =& ConnectionManager::getDataSource('default');
    if (empty($db->connection))
    {
            echo 'oh noes we werent able to connect';
            exit;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.