使用CI DataMapper ORM连接不同数据库中的表 [英] Joining tables in different databases with the CI DataMapper ORM

查看:125
本文介绍了使用CI DataMapper ORM连接不同数据库中的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实在寻找解决方案,但没有发现任何帮助. 问题是关于将多个数据库与datamapper一起使用,但我已经使用过方法.

I did search for a solution, but found nothing helpfull. This question was about using multiple databases with datamapper but I already use that method.

用户模型:

class User extends DataMapper {
    var $prefix = "app_";

    var $db_params = 'default';
...

公司型号:

class Company extends DataMapper {
    var $prefix = "tbl_";

    var $db_params = 'super';
...

我有以上型号.它们被关联为用户拥有一个公司和公司拥有许多用户. 当我创建公司时,将自动创建一个用户,并且成功建立关系.

I have the above models. They are related as user-has-one-company and company-has-many-user. When I create a company, a user will be created automatically and the relationship is made with succes.

但是,当我尝试删除公司或尝试访问用户的相关公司时. DataMapper在错误的位置查找用户表.

However, when I try to delete a company, or try to acces the related company of a user. DataMapper looks for the user table in the wrong place.

我遇到以下错误:

A Database Error Occurred

Error Number: 1146

Table 'a1210alf.app_users' doesn't exist

SELECT `tbl_companies`.* FROM (`tbl_companies`) LEFT OUTER JOIN `app_users` app_users ON `tbl_companies`.`id` = `app_users`.`company_id` WHERE `app_users`.`id` = 4

在我的CI配置中,我对两个数据库默认"和超级"有两个不同的设置.我在模型中设置了正确的模型.表app_users确实存在,所需的所有字段都在那里.不知道我在做什么错.

In my CI config I have two different settings for two databases 'default' and 'super'. And I set the correct one in the model. The table app_users does exist, all fields needed are there. Not sure what I am doing wrong.

推荐答案

我已经使用了一段时间了.无论如何,它仍然没有中断" ..所以我想这是正确的方法.

I'm using this solution for a while now. It still didn't 'broke' in any case .. so I guess it's the correct way to do it.

首先,我创建了一个扩展了"DataMapper"的"BaseModel".构造函数如下所示:

First I created a 'BaseModel' which extends 'DataMapper'. The constructor looks like this:

/**
 * Constructor: calls parent constructor
 */
function __construct($id = NULL, $db = NULL)
{
    if ($db != NULL && $this->db_params == '') {    
        // use these params as default
        $this->db_params = array_merge(array( 
            'dbdriver'  => 'mysql',
            'pconnect'  => true,
            'db_debug'  => true,
            'cache_on'  => false,
            'char_set'  => 'utf8',
            'cachedir'  => '',
            'dbcollat'  => 'utf8_general_ci',
            'autoinit'  => true,
            'stricton'  => false,
        ), $db);
    }
    elseif($this->db_params == '') {
        $CI =& get_instance();
        $cid = $CI->session->userdata('cid');
        if($cid != false || $cid != '') {
            $userDB = new Database();
            $userDB->get_where(array('company_id'=>$cid));
            if($userDB->exists()) {
                $this->db_params = array(
                    'hostname'      => $userDB->dbhost,
                    'database'      => $userDB->dbname,
                    'username'      => $userDB->dbuser,
                    'password'      => $userDB->dbpass,
                    'dbdriver'  => 'mysql',
                    'pconnect'  => true,
                    'db_debug'  => true,
                    'cache_on'  => false,
                    'char_set'  => 'utf8',
                    'cachedir'  => '',
                    'dbcollat'  => 'utf8_general_ci',
                    'autoinit'  => true,
                    'stricton'  => false,
                );
            }
        }
    }
    parent::__construct($id);
}

现在,如果创建用户实例,则应该执行$u = new User(NULL, $this->udb);,其中$this->udb是主控制器中加载的连接参数.

Now if you create an instance of a user we should do $u = new User(NULL, $this->udb); where $this->udb are the connection params which are loaded in the main controller.

这样,模型每次都会获取正确的参数.并且,如果需要相关对象,BaseModel会知道从何处获取正确的参数.

This way the model will get the correct params each time. AND if a related object is needed the BaseModel knows where to get the correct params.

我只关心它能起作用:p

All I care about is that it works :p

注意:无需在每个模型中设置var $db_params = 'default';.如果这样做,它将覆盖您在BaseModel构造函数中设置的参数.

NOTE: there is no need to set var $db_params = 'default'; in each model. If you do, it will overwrite the params you set in the constructor of BaseModel.

也许这对某人有用.但这是一个特例,如果您使用的是CodeIgniter + Datamapper,我不鼓励任何人以此方式设计数据库.

Maybe this will be usefull to someone. But this was a special case and I don't encourage anyone to design their database this way if you are using CodeIgniter + Datamapper.

这篇关于使用CI DataMapper ORM连接不同数据库中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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