更改cakephp控制器中的默认数据库

时间:2013-10-21 10:32:12

标签: php database cakephp

使用CakePHP版本:2.3.1

我有一个用于授权的默认数据库,基于此,不同客户端的不同数据库(数据库名称中包含客户端名称)。

我有很多模型,它们之间有各种各样的关系。我想使用一个调用来检索它们(它递归地检索所有相关的模型数据)。

场景:

数据库

default :
clients 
[id, password, name]
[1, 'qwertycolemak', 'amazon']
[2, '5t4ck0verfl0w', 'ebay']

非默认数据库(下2)

client_amazon
students[student_id, student_name]
course_students [student_id, course_id]
courses [course_id, course_name]

client_ebay
(same as client_amazon)

现在,假设我收到了[id:2,密码:'5t4ck0verfl0w']的请求 我检查默认数据库(客户端),检查密码,检索名称,在这种情况下 ebay

现在,我想访问的数据库是'client_ebay'

我在database.php中有不同的配置对应每个客户端。 我尝试使用

更改数据源
$this->student->setDatasource('client_ebay')
$this->course->setDatasource('client_ebay')
$this->course_student->setDatasource('client_ebay')

这适用于对模型的单独CRUD调用(非递归)。

但是当我使用调用(带递归)时,如

$this->student->findById(5)

datasource默认为'default',我收到错误:
在数据源默认 中找不到模型学生的表学生

如何通过控制器动态更改所有模型的默认数据源(不是逐个)?

3 个答案:

答案 0 :(得分:2)

模型层对从Model::getDataSource()返回的对象执行所有数据库操作,因此在AppModel类中覆盖该方法,并在必要时在那里设置正确的数据源可能是一个选项。

这是一个(未经测试的)示例,如果需要,这将把数据源设置为Model.globalSource中配置的任何数据源(如果设置了一个值)。只要可以调用Configure::write(),就可以更改该值。

...

class AppModel extends Model
{
    ...

    public function getDataSource()
    {
        $source = Configure::read('Model.globalSource');
        if($source !== null && $source !== $this->useDbConfig)
        {
            $this->setDataSource($source);
        }

        return parent::getDataSource();
    }

    ...
}

在您的控制器中,您可以执行类似

的操作
Configure::write('Model.globalSource', 'client_ebay');
$student = $this->Student->findById(5);

答案 1 :(得分:0)

您是否尝试修改模型的$ useDbConfig属性?例如:

$this->Student->useDbConfig = 'client_ebay';

答案 2 :(得分:0)

控制器中的示例,在CakePHP 2.5.x中为DataSources更改多个数据库

App::uses('AppController', 'Controller');

class DemoController extends AppController {

public $uses = array('AppModel', 'GVA21', 'GVA01', 'GVA14', 'GVA24' );
public function test_dbs(){
    $this->autoRender=false;
    // Load ConnectManager
    App::uses('ConnectionManager', 'Model');
    // DataSource ['default']
    $MDM = $this->GVA14->find('count');
    echo "MDM.GVA14\n<br>";
    debug($MDM);
    // Get DataSource Config DB ['default'] and ['SRL']
    $confDeafult = ConnectionManager::$config->default;
    $confSrl = ConnectionManager::$config->SRL;
    // Change DataSource ['SRL']
    ConnectionManager::drop('default');
    ConnectionManager::create('default',$confSrl);  //<== Is permanet change Find All models Down
    // $this->GVA01->setDataSource('SRL'); //<== Is temp change Find model
    echo "SRL.GVA14\n<br>";
    $SRL = $this->GVA14->find('count');
    debug($SRL);
    $SRL = $this->GVA01->find('count');
    echo "SRL.GVA01\n<br>";
    debug($SRL);
    $SRL = $this->GVA21->find('count');
    echo "SRL.GVA21\n<br>";
    debug($SRL);
    // Change to DataSource ['default']
    debug(ConnectionManager::drop('default'));
    ConnectionManager::create('default',$confDeafult); //<== Is permanet change Find All models Down
    //$this->GVA01->setDataSource('default'); //<== Is temp change Find model
    $MDM = $this->GVA01->find('count');
    echo "MDM.GVA01\n<br>";
    debug($MDM);
    $MDM = $this->GVA21->find('count');
    echo "MDM.GVA21\n<br>";
    debug($MDM);
    ////FIN
    exit('FIN');
}
相关问题