简体   繁体   English

CakePHP在CakePHP 2.2.5控制器中切换数据库连接

[英]Cakephp switch database connection in controller for CakePHP 2.2.5

I am trying to get data from two different databases in a controller 我试图从控制器中的两个不同的数据库中获取数据

app/Controller/UsersController.php 应用程序/控制器/ UsersController.php

my db connections are declared in the database.php 我的数据库连接在database.php中声明

$default = array(
    ...
    'database' => 'test'
    ...
    );
$test = array(
    ...
    'database' => 'test1'
    ...
    );

and in my display() action: 并在我的display()动作中:

public function display() {
    $this->set('defaultUsers', $this->User->find('all'));
    $this->User->schemaName = 'test1';
    $this->User->cacheQueries = false;
    $this->set('testUsers', $this->User->find('all'));
}

This would allow me to grab data from two different sources successfully, however problem is that these two databases have to have the same password otherwise it wouldn't work. 这将允许我成功地从两个不同的源获取数据,但问题是这两个数据库必须具有相同的密码,否则它将无法工作。

I've tried other solutions found here and other sites. 我尝试过在这里和其他网站找到的其他解决方案。 like: 喜欢:

  • changing $this->User->useDbConfig = 'test' and $this->User->cacheQueries = false would still give me the same dataset; 更改$this->User->useDbConfig = 'test'$this->User->cacheQueries = false仍会给我相同的数据集;

  • using ConnectionManager::getDataSource() and setConfig() , create() , drop() , setDataSource() , etc. None of those worked and some don't even exist any more. 使用ConnectionManager::getDataSource()setConfig()create()drop()setDataSource()等。这些都不起作用,有些甚至不再存在。

Any help would be greatly appreciated! 任何帮助将不胜感激! As I need to the same codebase for two similar applications aka two databases. 因为我需要为两个类似的应用程序(即两个数据库)使用相同的代码库。

Thanks! 谢谢!

You probably need to use 'setDataSource()' to switch the datasource/connection as this will reset the 'cached' schema etc on the Model; 您可能需要使用'setDataSource()'来切换数据源/连接,因为这将重置模型上的'缓存'模式等;

public function display() {
    $this->set('defaultUsers', $this->User->find('all'));
    $this->User->setDataSource('test1');
    $this->set('testUsers', $this->User->find('all'));
}

If you need to access data from both databases throughout your application, another option is to create two User models, one with 'test' as datasource, and another that uses 'test1' as datasource. 如果需要在整个应用程序中访问两个数据库中的数据,另一个选项是创建两个用户模型,一个用'test'作为数据源,另一个用'test1'作为数据源。 This way you don't have to switch the data-source all the time 这样您就不必一直切换数据源

Example: 例:

class TestUser extends AppModel {
    public $useTable = 'users'; // name of the database table 
    public $useDbConfig = 'test1';  // name of the database configuration in database.php
}

On a further note: The '$test' database-config is pre-configured database-connection that is used for running CakePHP Unit-tests. 另请注意:'$ test'database-config是预先配置的数据库连接,用于运行CakePHP单元测试。 You might consider creating your own database-connection name 您可以考虑创建自己的数据库连接名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM