I have an application in CakePhp 1.2, where depends on the domain, SOME of the models have to change dinamically the database.

So I need to find an easy way to:

  1. Check the domain.
  2. Set $useDbConfig on SOME models the database required.

This function on database.php change the database depending just on the domain, but not on the model:

public function __construct(){

if (strpos(env('HTTP_HOST'), 'site_one') !== false) {

  // use site_one database config 
  $this->default = $this->site_one;

} elseif (strpos(env('HTTP_HOST'), 'site_two') !== false) {

  // use site_two database config 
  $this->default = $this->site_two; } 

}

How can I change the database depending also on the model?

Thanks in advance.

有帮助吗?

解决方案

What if you used your code, but had an additional database config for those models that don't change? This additional database config would not be changed by the function you posted. In those models that you don't want to change, add the line

var $useDbConfig = 'static'; 

or whatever the name of your database config is that doesn't change. Then those that do change, you leave using the default config.

其他提示

write in your database.php

var $default = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => '',
    'database' => 'first-database-name',
    'prefix' => '',
);

 var $otherdatabase = array(
    'driver' => 'mysql',
    'persistent' => false,
    'host' => 'localhost',
    'login' => 'root',
    'password' => '',
    'database' => 'other-database-name',
    'prefix' => '',
);

and in model

class Modelname extends AppModel {

    var $name = 'Modelname';
    var $useTable = 'tablename from other-database';  
    var $useDbConfig = 'otherdatabase ';
    .......... .......... .........
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top