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

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

问题描述

好的,我的第一个问题修改了这么多次,我选择删除它,重新编写我的问题。我已经做了一个小的测试用例项目,用不同的模型名称为我的问题找到一个正确的解决方案。



警告数据库表格混合



动机数据转换成多个数据库



目前我正在开发一个 CakePHP 项目,它有多个 User cars ),并且每个用户 code>是表格之一)。现在,我需要先解释一下:



每个用户都有自己的数据库 (不是表格,数据库),因此数据库名称如下。




  • [DATABASE] app (这是应用程式的主要资料库)

    • [TABLE] users

    • [TABLE] 权限不适用于此问题


  • [DATABASE] app_user1 ( User.id 拥有整个数据库

    • [TABLE] cars 完全由 User.id 1)


  • DATABASE] app_user2 User.id 2拥有此整个数据库

    • [TABLE] cars (由 User.id 2)


  • 等...



我制作了一个小图,可以澄清数据库 / - 定义及其与模型的关系:





问题!!!



我不知道哪个 和他们的数据库之间建立连接,直到 User 动态创建,所以我不能使用 app / Config / database.php



扩展模型 ConnectionManager 类以绕过CakePHP的基本数据库行为。因此, Car 模型知道要使用哪个数据库。但我只是有一种感觉,这可以做得更容易!



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



有更简单的方法吗?



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

解决方案

此绅士 Olivier )有同样的问题! (一年前)他写了 控制器的小适应!它非常小,事实证明,它工作在 1.3 2.x



我的最终解决方案,我放在 app / Model / AppModel.php

  class AppModel extends Model 
{
/ **
*连接到指定的数据库
*
* @param要连接的不同数据库的字符串名称。
* @param现有数据源的字符串名称
* @return boolean成功时为true,失败时为false
* @access public
* /
public function setDatabase数据库,$ datasource ='默认')
{
$ 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;
}
}

这里是我如何使用它< c $ c> 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);
}

}

m不是第一个或最后一个与这个问题。所以我真的希望这个信息会找到人和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天全站免登陆