Monday, April 15, 2013

Dynamic Database Switching

Hello All.

 

I am trying to switch the database at runtime when the user logs in. There are two approaches that I've tried:

 

   replace the 'default' database name

   and create another configuration name

 

but neither approach seems to work.

 

Is there a specific order of operations? For example, assume the 'default' is connected, and when I need to switch databases, the steps I take are:

 

   ConnectionManager::getDataSource( 'default' )->disconnect();

   $ds = ConnectionManager::getDataSource( 'default' );

   $ds->config['database'] = $dbfName;

   $ds->connect();

 

Unfortunately this doesn't work.

 

My other approach is similar:

 

   ConnectionManager::getDataSource( 'default' )->disconnect();

   $settings = array( 'datasource' => 'Database/Mysql',

                               'persistent' => false,

                               'host' => 'localhost',

                               'login' => 'root',

                               'password' => '',

                               'database' => $databasename,

                               'prefix' => '' );

                              

   ConnectionManager::create( 'company', $settings );

           

   $ds = ConnectionManager::getDataSource( 'company' );

   $ds->connect();

 

 

With either of these approaches I get [MissingTableException].

 

I've tried setDataSource for each Model:

 

   $this->ModelNameHere>setDataSource( 'company' );

 

But no luck.

 

I forget the exact version of CakePHP that I am using, but I downloaded it from github a few weeks back.

 

Thank you for your help.

 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: