Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp 1.3 datasource change issue

I am using CakePHP 1.3 with two data sources:

  1. master
  2. default

How can I change the data source for a whole function i.e. from default to master for that function.

If I use this

$this->{Model}->setDataSource('master');

it only changes for that model.

Is there any general way???

like image 663
Bhupendra Pandey Avatar asked Feb 04 '26 15:02

Bhupendra Pandey


1 Answers

OK. Try doing something like this. I tried it in newer version of cake and its worked.

in your app model have a function

public function getDataSource(){
    $dataSource = Configure::read('currentDataSource');
    if($dataSource && $dataSource !== $this->useDbConfig){
        $this->setDataSource($dataSource);
    }
    return parent::getDataSource();
}

Then in your controller , based which data source needed write it in configure.

Configure::write('currentDataSource', 'yourDbsource');

Then call to any of your models.

Dont forget to specify default one from bootstrap.php.

like image 126
Arman Avatar answered Feb 06 '26 06:02

Arman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!