简体   繁体   English

Codeigniter Datamapper跨数据库连接

[英]Codeigniter Datamapper Cross-Database Joins

Good morning, 早上好,

I am currently working on a project that utilises Codeigniter and the Datamapper Library (http://datamapper.wanwizard.eu). 我目前正在开发一个利用Codeigniter和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"). 该项目使用一个用于数据的中央数据库(称为“base”)和一个用于应用程序数据的单独数据库(“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. 我有一个问题,我需要使用内置的Datamapper关系,但跨两个数据库。 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: 当我尝试使用$ 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

Could anybody shed some light on the matter? 有人可以解释一下这个问题吗? Does DataMapper actually support cross-database joins. DataMapper是否实际支持跨数据库连接。 If not, is there any way that I can achieve this? 如果没有,有什么办法可以实现这个目标吗?

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). 您不能跨数据库执行此操作,您甚至可以从SQL中看到它正在尝试查询单个数据库(缺少。表示数据库甚至)。 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. 在这种情况下,您可以尝试编写自定义SQL查询(不确定在您的数据库上),但在查询中定义database.table关系,并尝试使用简单的语句。

Otherwise you will need to solve this conundrum programmatically via objects / arrays / etc; 否则你需要通过objects / arrays / etc以编程方式解决这个难题;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM