How do I get the values ​​for the database configuration from some database tables in CakePHP?

I am making an application that will be installed on multiple clients and the database setup for each client will be different as I will be using several different LIKE Oracle and MySql databases. One database will be common for all, I made a table in the same place where I will save db configuration data, now how to select this data from the table in database.php.

Can't find anything confusing. `class DATABASE_CONFIG {

public $default = array(
    'datasource' => 'Database/Mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => 'root',
    'database' => 'hrportal_imp',
    'prefix' => '',
    //'encoding' => 'utf8',
); 
//want to fetch data from x table from Default datasource.
public $ora = array(
    'datasource' => 'Database/Oracle',
            'persistent' => false,
            'host' => '<IP i get from above db>',
            'port' => '1521',
            'login' => '<Data i get from above db>',
            'password' => '<Data i get from above db>',
    'database' => '<IP i get from above db>:1521/orcl',
            'prefix' => '',
    'sid' => 'orcl'

);

      

} `

+3


source to share


2 answers


I did it by placing the same in BeforeFilter from AppController.



App::import('Model', 'ConnectionManager');
        ConnectionManager::create('ora',
        $config = array('datasource' => 'Database/Oracle',
                        'persistent' => false,
                        'host' => 'dynamic Host',
                        'port' => '1521',
                        'login' => 'HCM',
                        'password' => 'hdhd',
                        'database' => 'dynamic host:1521/dhdh',
                        'prefix' => '',
                        'sid' => 'orcl')
            );

      

+4


source


by default, cakephp will use default configurations, which you can write in your $ default variable. you can also change the database connection as required. you can also check your currently selected database on your controller.



App::import('Model', 'ConnectionManager');
$ds = ConnectionManager::getDataSource('default');
echo $ds->config['database'];

      

+1


source







All Articles