CakePHP 使用多个数据库作为模型 [英] CakePHP using multiple databases for models

查看:22
本文介绍了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 使用多个数据库作为模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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