Codeigniter Datamapper 跨数据库连接

发布于 2024-12-09 02:54:09 字数 1351 浏览 0 评论 0原文

早上好,

我目前正在开发一个使用 Codeigniter 和 Datamapper 库 (http://datamapper.wanwizard.eu) 的项目。该项目使用一个用于用户数据的中央数据库(称为“base”)和一个用于应用程序数据的单独数据库(“crm”)。这样,将来我们可以构建使用用户目录的其他应用程序,而无需绑定到单个服务器。

我有一个问题,因为我需要使用内置的 Datamapper 关系,但跨两个数据库。这是我到目前为止的设置:

人员模型

class person extends DataMapper {

    var $db_params = 'base';
    var $prefix = 'base_';

    var $has_many = array(
        'initiated_event' => array('class' => 'event','other_field' => 'initiator')
    )

}

事件模型

class event extends DataMapper {

    var $db_params = 'crm';
    var $prefix = 'crm_';

    var $has_one = array(
        'initiator' => array('class' => 'person','other_field' => 'initiated_event')
    )

}

问题

当我尝试使用 $events->initiator-> ;get(),我收到以下错误:

Error Number: 1146
Table 'circle_base.crm_events' doesn't exist
SELECT `base_persons`.* FROM (`base_persons`) LEFT OUTER JOIN `crm_events` initiated_event_crm_events ON `base_persons`.`id` = `initiated_event_crm_events`.`initiator_id` WHERE `initiated_event_crm_events`.`id` = 1
Filename: C:\xampp\htdocs\circle\crm\system\database\DB_driver.php
Line Number: 330

有人能解释一下这个问题吗? DataMapper真的支持跨数据库连接吗?如果没有,有什么方法可以实现这一目标吗?

Good morning,

I am currently working on a project that utilises Codeigniter and the Datamapper Library (http://datamapper.wanwizard.eu). The project uses a central database for user data (called "base") and a seperate database for the application data ("crm"). This is so that in the future, we may build other applications that use the user directory without being tied to a single server.

I have a problem in that I need to use the built in Datamapper relationships but across the two databases. Here is my setup so far:

Person Model

class person extends DataMapper {

    var $db_params = 'base';
    var $prefix = 'base_';

    var $has_many = array(
        'initiated_event' => array('class' => 'event','other_field' => 'initiator')
    )

}

Event Model

class event extends DataMapper {

    var $db_params = 'crm';
    var $prefix = 'crm_';

    var $has_one = array(
        'initiator' => array('class' => 'person','other_field' => 'initiated_event')
    )

}

The Problem

When I attempt to use $events->initiator->get(), I get the following error:

Error Number: 1146
Table 'circle_base.crm_events' doesn't exist
SELECT `base_persons`.* FROM (`base_persons`) LEFT OUTER JOIN `crm_events` initiated_event_crm_events ON `base_persons`.`id` = `initiated_event_crm_events`.`initiator_id` WHERE `initiated_event_crm_events`.`id` = 1
Filename: C:\xampp\htdocs\circle\crm\system\database\DB_driver.php
Line Number: 330

Could anybody shed some light on the matter? Does DataMapper actually support cross-database joins. If not, is there any way that I can achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒解孤独 2024-12-16 02:54:09

您无法跨数据库执行此操作,您甚至可以从 SQL 中看到它正在尝试查询 1 个数据库(甚至缺少数据库的 . 符号)。我假设您的两个数据库都在同一服务器上?在这种情况下,您可以尝试编写自定义 SQL 查询(不确定您的数据库),但在查询中定义 database.table 关系,并尝试使用简单的语句。

否则,您将需要通过对象/数组/等以编程方式解决这个难题;

参考资料:
http://nathan.rambeck.org/blog/2 -跨多个数据库连接 mysql-表

You can't do this across databases, you can even see from your SQL that it is trying to query 1 single database (lack of . notation for database even). I am assuming both your databases are in the same server? In which case you could try writing a custom SQL query (not sure on your DBs) but in the query define the database.table relationship, and try with a simple statement.

Otherwise you will need to solve this conundrum programmatically via objects / arrays / etc;

References:
http://nathan.rambeck.org/blog/2-joining-mysql-tables-across-multiple-databases

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文