【问题标题】:Change default database in cakephp controller在 cakephp 控制器中更改默认数据库
【发布时间】:2014-10-31 08:55:44
【问题描述】:

使用 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, password:'5t4ck0verfl0w'] 的请求 我检查默认数据库(clients),检查密码,检索名称,在本例中为 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)

数据源默认为“默认”,我收到一个错误:
模型 student 的表 students 未在数据源 default 中找到

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

【问题讨论】:

  • 请务必提及您的确切 CakePHP 版本! ps,有什么理由需要使用单独的数据库吗?
  • 当你改变你的来源时,它是持久的吗?还是在第一次查询后重置为默认值?我会调查一下
  • 添加了 cakePHP 版本 @ndm 我正在为不同的客户处理交易,所以我需要将它们分开
  • @AyoAkinyemi 更改源对于单个模型肯定是持久的 问题仅在 cakePHP 尝试递归检索数据库关系方面的关联数据时才会出现。然后,它会自动切换到默认值。
  • 明白了!如何在您的 appModel 类中编写一个函数,将源设置为您想要的源,当您完成查询后,使用另一个函数将其重置为默认值。因为无论如何所有模型都会通过 appModel,所以该函数将始终确保任何模型都使用您设置的数据源。

标签: php database cakephp


【解决方案1】:

模型层对从 Model::getDataSource() 返回的对象执行所有 DB 操作,因此在您的 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);

【讨论】:

  • 你是一个天才和救星。非常感谢!
  • 我们可以将它用于useDbConfig而不是setDataSource吗?
  • @Anupal 仅更改 $useDbConfig 属性可能还不够,因为这会使架构名称和表前缀保持不变。
  • 尤其是当我做 App::import 时
  • @ndm 为什么你认为模式名和表前缀没有改变?如果我使用defaultdatabase.php 中的default 数据源中使用的所有基本参数创建像ConnectionManager::create() 这样的新连接。
【解决方案2】:

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

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

【讨论】:

  • 是的,我已经单独和一起尝试了这两种方法(useDbConfig 和 setDatasource)。
【解决方案3】:

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

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');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 2013-02-23
    • 2016-11-21
    • 1970-01-01
    相关资源
    最近更新 更多