Martin Bean
Martin Bean

Reputation: 39389

How to specify database configuration in CakePHP 2.0.2?

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',
    );
}

Upvotes: 3

Views: 16136

Answers (2)

burriko
burriko

Reputation: 413

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;
    }
}

Upvotes: 16

dhofstet
dhofstet

Reputation: 9964

Your database configuration is very likely correct.

The reason "Cake is NOT able to connect to the database." is shown, is because the script that checks whether it can connect to the database (/lib/Cake/View/Pages/home.ctp) only uses the default database connection for this test. And as there is no such connection, it fails.

Upvotes: 2

Related Questions