CakePHP使用多个数据库来建模 [英] CakePHP using multiple databases for models

查看:93
本文介绍了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 )是 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天全站免登陆