How to specify database configuration in CakePHP 2.0.2?

Martin Bean picture Martin Bean · Nov 3, 2011 · Viewed 14.1k times · Source

I've just installed CakePHP 2.0.2 for use in a new project. I'm trying to use a database configuration called development but my model doesn't seem to be picking it up.

Based on CakePHP 2's new directory and file name conventions, I've created the following at /app/Model/AppModel.php:

<?php
class AppModel extends Model {

    public $useDbConfig = 'development';
}

However, the default home page tells me:

Cake is NOT able to connect to the database.

Yet if I change the configuration name in /app/Config/database.php to default the message changes to a success message, as though it's not picking up my custom AppModel class.

How can I remedy this? As the new CakePHP 2.0 docs say to use the $useDbConfig property as I have done above?

EDIT: Contents of /app/Config/database.php:

class DATABASE_CONFIG {

    public $development = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'root',
        'password' => '',
        'database' => 'cakephp_db',
        'prefix' => '',
        'encoding' => 'utf8',
    );
}

Answer

burriko picture burriko · Nov 3, 2011

Like dhofstet has explained, you still need to have a default config. What I do is add a constructor to the DATABASE_CONFIG class to switch between database configs.

Something like this...

public function __construct()
{
    if (DEV_SERVER) {
        $this->default = $this->development;
    } else {
        $this->default = $this->production;
    }
}