CakePHP 使用多个数据库作为模型

CakePHP using multiple databases for models(CakePHP 使用多个数据库作为模型)
本文介绍了CakePHP 使用多个数据库作为模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

某些模型是否可能在一个数据库中而其他模型在另一个数据库中(使用相同的连接)?

Is it possible for certain models to be in one database and other models in another (using the same connection)?

我有许多只读表,希望在系统的多个安装之间共享.其他表需要每次安装.例如,假设 users 是共享表,而 posts 是每次安装.

I have a number of read-only tables that I want shared between multiple installations of my system. Other tables need to be per-installation. For the sake of example, let's say users is the shared table, and posts is per-installation.

在一个模式(我们称之为共享")中,我们有 users 表,而在另一个模式(mycake")中是 posts.

In one schema (let's call it "shared") we have the users table, and in another schema ("mycake") is posts.

通过创建一个指向共享数据库的新数据库连接,我已经能够从另一个数据库中读取用户模型(尽管这两个数据库位于同一主机上并且都可以使用相同的登录详细信息访问).

I've been able to get the User model reading from the other database by creating a new database connection which points to the shared database (though the two databases are on the same host and are both accessible with the same login details).

class User extends AppModel {
    var $useDBConfig = 'sharedConnection';
}

问题在于何时加入posts 表.它不会在表名前面添加模式名称,因此它找不到 posts.

The problem is when it comes time to join to the posts table. It doesn't prepend the schema name to the table name, and so it can't find posts.

// what it does
SELECT * FROM users User INNER JOIN posts Post ...

// what I'd like it to do:
SELECT * FROM shared.users User INNER JOIN mycake.posts Post ...

所以基本上,有没有办法为模型分配一个完全限定的表名并强制它在所有查询中使用它?设置 var $useTable = 'shared.users'; 没有帮助...

So basically, is there a way to assign a fully qualified table name to a model and force it to use that in all its queries? Setting var $useTable = 'shared.users'; doesn't help...

推荐答案

这个网站有办法做到这一点,但有点老套.

This site has a way to do it, but it's a little hacky.

这篇关于CakePHP 使用多个数据库作为模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Codeigniter htaccess to remove index.php and www(Codeigniter htaccess 删除 index.php 和 www)
htaccess mod_rewrite part of url to GET variable(htaccess mod_rewrite url 的一部分到 GET 变量)
Replacing a querystring parameter value using mod_rewrite(使用 mod_rewrite 替换查询字符串参数值)
.htaccess in subdirectory #39;overriding#39; parent htaccess(子目录“覆盖父 htaccess 中的 .htaccess)
How to rewrite SEO friendly url#39;s like stackoverflow(如何像stackoverflow一样重写SEO友好的url)
Is it okay to have a very long .htaccess file?(有一个很长的 .htaccess 文件可以吗?)