php - CakePHP how to assign database connection dynamically -
i have constant value called client_url
on boostrap:
$clienturl = explode('/', $_server['request_uri']); define("client_url",$clienturl[2]);
i've debugged , gets, example, 'client1', or 'client2', etc. need use value when defining configuration array, such as:
on database.php
class database_config { var $default = array( 'datasource' => 'database/mysql', 'persistent' => false, 'host' => 'localhost', 'login' => '*****', 'password' => '****', 'database' => 'client3', 'prefix' => '' ); var $clientdb = array( 'datasource' => 'database/mysql', 'persistent' => false, 'host' => 'localhost', 'login' => '****', 'password' => '****', 'database' => 'client3', 'prefix' => '' ); public function __construct() { debug('constant value: '.client_url); debug($this->default['database']); $this->default['database'] = client_url; // line nothing !! //$this->clientdb['database'] = client_url; // lines prevent connection, error: database connection "mysql" missing, or not created !! $this->default = $this->clientdb; debug($this->default['database']); } };
however, database remains unchanged, keeps initial value above. can do:
$this->default['database'] = 'whatever';
and won't try find 'whatever' database; seems assignment missing reload or something?
after assignment on __construct,
i've debugged
debug($this->default['database']);
before , after assignment , not change. changes doing this:
$this->clientdb['database'] = client_url; // instead of $this->default['database'] = client_url;
but connection fails.
please note app connect database given @ beginning ('client3'), don't switch. check database via find, return same record same database, no switch.
$options = array('conditions' => array('user.id' => 1)); $clientarray = $this->user->find('first', $options);
thank pointers.
you need
class database_config { var $client1db = array( 'datasource' => 'database/mysql', 'persistent' => false, 'host' => 'localhost', 'login' => '*****', 'password' => '*****', 'database' => 'client1', 'prefix' => '' ); public function __construct() { $this->default = $this->clientdb; $this->default['database'] = client_url; } };
Comments
Post a Comment