CakePHP中如何为一个模型动态使用多个数据库 [英] How to use multiple databases dynamically for one model in CakePHP

查看:28
本文介绍了CakePHP中如何为一个模型动态使用多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我的第一个问题被修改了很多次,我选择删除它并重新表述我的问题.我制作了一个具有不同模型名称的小型测试用例项目,以便为我的问题找到合适的解决方案.

警告:不要将数据库表格

混用

动机:我将用户数据分成多个数据库用于法律和性能问题.

目前我正在开发一个 CakePHP 项目,该项目有多个 User 并且每个 User 都有自己的数据库 带有多个表格(cars表格之一).现在,我需要先解释一下:

每个User都有自己的数据库(不是表,数据库),所以数据库名称如下.

  • [DATABASE] app(这是应用的主数据库)
    • [TABLE] 用户
    • [TABLE] permissions(与此问题无关)
  • [DATABASE] app_user1(User.id 1 拥有整个数据库)
    • [TABLE] cars(User.id 1 完全拥有的table)
  • [DATABASE] app_user2(User.id 2 拥有整个数据库)
    • [TABLE] cars(User.id 2 完全拥有的table)
  • 等等...

我画了一张小图,可以阐明数据库/表格-定义及其与模型的关系:

问题!!!

User 登录之前,我不知道要连接到哪个数据库.User 及其数据库 是动态创建的,所以我不能使用 app/Config/database.php.

所以我目前正在为 ModelConnectionManager 类编写扩展,以绕过 CakePHP 的基本数据库行为.所以Car 模型知道使用哪个数据库.但我只是觉得这可以更容易!

所以我想这一切都归结为一个问题:

有更简单的方法吗?!

感谢任何愿意花时间和精力阅读和理解我的问题的人!

解决方案

这位先生 (Olivier) 遇到了同样的问题!(一年前)他写道Controller 的小改动!它非常小,事实证明,它适用于 1.32.x.

无论如何,这是我的最终解决方案,我将其放入app/Model/AppModel.php:

class AppModel 扩展模型{/*** 连接到指定的数据库** @param String 要连接的不同数据库的名称.* @param String 现有数据源的名称* @return boolean true 成功,false 失败* @access 公开*/公共函数 setDatabase($database, $datasource = 'default'){$nds = $datasource .'_' .$数据库;$db = &ConnectionManager::getDataSource($datasource);$db->setConfig(array('名称' =>$nds,'数据库' =>$数据库,'持久' =>错误的));if ( $ds = ConnectionManager::create($nds, $db->config) ) {$this->useDbConfig = $nds;$this->cacheQueries = false;返回真;}返回假;}}

这是我在我的 app/Controller/CarsController.php 中使用它的方式:

class CarsController 扩展了 AppController{公共函数索引(){$this->Car->setDatabase('cake_sandbox_client3');$cars = $this->Car->find('all');$this->set('cars', $cars);}}

我敢打赌,我不是第一个或最后一个遇到这个问题的人.所以我真的希望这些信息能找到人们CakePHP 社区.

Ok, my first question was modified so many times that I chose to delete it and reformulate my question. I've made a small test-case project with different model-names to find a proper solution for my problem.

Warning: Don't mix the databases up with tables

Motivation: I separated the user-data into multiple databases for legal & performance issues.

Currently I'm working on a CakePHP project that has multiple User's and each User has it's own database with multiple tables (cars is one of the tables). Now, I need to explain something first:

Every User has his own database (not a table, a database), so the database names are as follows.

  • [DATABASE] app (This is the app's main database)
    • [TABLE] users
    • [TABLE] permissions (Not relevant for this question)
  • [DATABASE] app_user1 (User.id 1 owns this entire database)
    • [TABLE] cars (a table entirely owned by User.id 1)
  • [DATABASE] app_user2 (User.id 2 owns this entire database)
    • [TABLE] cars (a table entirely owned by User.id 2)
  • etc...

I made a small drawing which might clarify the database / table -definitions and their relations to the models:

The problem!!!

I don't know which database to connect to until the User logs in. User's and their databases are created dynamically, so I cant use app/Config/database.php.

So I'm currently writing an extension on the Model and ConnectionManager classes to bypass CakePHP's basic database behaviors. So the Car model knows which database to use. But I just have a feeling this could be done easier!

So I guess it all boils down to one question:

Is there an easier way of doing this?!

Thanks to anyone who will take the time and effort of reading and understanding my problem!

解决方案

This gentleman (Olivier) had the same problem! (A year ago) He wrote a small adaptation for the Controllers! It's pretty small and it turns out, it works in 1.3 and 2.x.

Anyhow, this is my final solution, that I put in the app/Model/AppModel.php:

class AppModel extends Model
{
  /**
   * Connects to specified database
   *
   * @param String name of different database to connect with.
   * @param String name of existing datasource
   * @return boolean true on success, false on failure
   * @access public
   */
    public function setDatabase($database, $datasource = 'default')
    {
      $nds = $datasource . '_' . $database;      
      $db  = &ConnectionManager::getDataSource($datasource);

      $db->setConfig(array(
        'name'       => $nds,
        'database'   => $database,
        'persistent' => false
      ));

      if ( $ds = ConnectionManager::create($nds, $db->config) ) {
        $this->useDbConfig  = $nds;
        $this->cacheQueries = false;
        return true;
      }

      return false;
    }
}

And here is how I used it in my app/Controller/CarsController.php:

class CarsController extends AppController
{
  public function index()
  {
    $this->Car->setDatabase('cake_sandbox_client3');

    $cars = $this->Car->find('all');

    $this->set('cars', $cars);
  }

}

I'm betting, I'm not the first or last one with this problem. So I really hope this information will find people & the CakePHP community.

这篇关于CakePHP中如何为一个模型动态使用多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆