php - CakePHP 2.x - Database Switching On The Fly (For The Whole App) -


background: have admin system, needs connect , edit multiple databases. database structures 100% similar each other, data varies.

what have tried: i'v tried use $this->model->setdatasource('db_variable_here'); in controllers change database on fly. problem this, related data seems still default database.

example: imagine this: user habtm post, if want post different database, , use $this->post->setdatasource('db_variable_here'); achieve this, seems still related user default database, , not same 1 got post from.

i'm guessing due fact change database on model post, fixed doing $this->model->setdatasource('db_variable_here'); each related model.

my question: possible change datasource every model in app on fly? ex. like: $this->setdatasource('datasource_name')? or have manually related models?

you try making own getdatasouce method in appmodel.

you can see cakephp 1 here: https://github.com/cakephp/cakephp/blob/master/lib/cake/model/connectionmanager.php

so, in appmodel, make sure accept/return da

class appmodel extends model {     //...      public function getdatasource() {          // logic here determine source want         // maybe use configure::write('mysource', 'other_datasource');         // somewhere else, check here.         $source = 'default';          $this->setdatasource($source);         return parent::getdatasource();     }      //...  } 

this should called instead of cakephp's 'getdatasource()', run checks determine connection use, calls cakephp's 'getdatasouce()' rest of actual work of getting data source.

assuming set variable (like configure variable) that's accessible here, set once anywhere in app, , when runs, use whatever you've specified.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -