Hello,
I read several threads about this but not found a final "cake-like"
solution. A lot of posts are about switching based on URLs, but I need
to switch to another "testcases" database connection based on a
session value I set in an action. This "testcases" connection should
be used until end of the session/logout.
I read about modifying database.php adding function __construct() and
switching the default connection there, but how can I access the cake
session there - I assume the cake core isn't fully initialized at that
construct point and I cannot use the session component there.
My setup:
- In database.php I created two items: $default and $testcases (only
difference: database name and content of course)
- In a controller action I am saving a session value which database
connection to use
Try 1) I tried to set in app_controller.php/beforeFilter() the model
var useDbConfig to switch the database. But after the normal action
and find('all) I get a SQL error (no database selected) whereas my
debug calls tell me that the model has switched to the "testcases"
database config correctly (or not?).
function beforeFilter() {
if ($this->Session->read('tmp.dbconfig') && is_object($this->
{$this->modelClass})) {
/* debugging */ $db = $this->{$this->modelClass}-
>getDataSource(); debug($db->config);
$this->{$this->modelClass}->useDbConfig = 'testcases';
/* debugging */ $db = $this->{$this->modelClass}-
>getDataSource(); debug($db->config);
}
...
}
Warning (512): SQL Error: 1046: No database selected [CORE\cake\libs
\model\datasources\dbo_source.php, line 525]
Try 2) I tried to use the ConnectionManager method in the
app_controller.php/beforeFilter(), but had no real success ... I do
not really know what to do! From another thread I tried the following
but same error
function beforeFilter() {
if ($this->Session->read('tmp.dbconfig') && is_object($this->
{$this->modelClass})) {
/* debugging */ $_db = $this->{$this->modelClass}-
>getDataSource(); debug($_db->config);
$db =& ConnectionManager::getDataSource('default');
$db->disconnect();
$db->config['database'] = 'db_testcases';
$db->connect();
/* debugging */ $_db = $this->{$this->modelClass}-
>getDataSource(); debug($_db->config);
}
...
}
Warning (512): SQL Error: 1046: No database selected [CORE\cake\libs
\model\datasources\dbo_source.php, line 525]
So, what is the best practice to switch database connections
dynamically by session content?
In controller, model, or???
I am very thankful for any positive answer!
Christian
ps. talking about cake 1.2.5
my references: searched for "usedbconfig session"
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---