3

我有一个项目必须跨越多个数据库。

一个数据库有表:

CREATE TABLE device {
  device_uid integer unsigned not null primary key auto_increment,
  os varchar(50),
  name varchar(50)
}

CREATE TABLE platform {
  platform_id integer unsigned not null primary key auto_increment,
  name varchar(50)
}

另一个数据库有表:

CREATE TABLE oses_platforms {
  platform_id integer unsigned not null primary key auto_increment,
  os varchar(50),
  platform_id integer unsigned
}

我为表和关系创建了模型:

<?php
class Platform extends AppModel {

    var $name = 'Platform';
    var $useTable = 'platform';
    var $primaryKey = 'platform_id';
    var $useDbConfig = 'otherdb';

    var $hasOne = array(
        'OsesPlatform' => array(
            'className' => 'OsesPlatform',
            'foreignKey' => 'platform_id'
        )
    );
}

class Device extends AppModel {

    var $name = 'Device';
    var $primaryKey = 'device_uid';
    var $useDbConfig = 'otherdb';        

}

class OsesPlatform extends AppModel {

    var $useDbConfig = 'default';
    var $name = 'OsesPlatform';
    var $primaryKey = 'os';


        var $belongsTo = array(
                'Platform' => array(
                        'className' => 'Platform',
                        'foreignKey' => 'platform_id',                            
                ),
        );

        var $hasMany = array(
                'Device' => array(
                        'className' => 'Device',
                        'foreignKey' => 'os',
                        'dependent' => false,                            
                )
        );
}
?>

如果所有三个表都驻留在“default”或“otherdb”中,我可以在从 Device 到 OsesPlatform 的 hasOne 或 belongsTo 关系的“conditions”参数中执行此操作,实际上 Platform 和 OsesPlatform 之间的 hasOne 关系工作正常。但是,我需要建模的是设备和平台之间的关系。

4

3 回答 3

1

事实证明,Cake 并未正式支持跨数据库的 HABTM 关系(来自 #cakephp 上的 markstory)。虽然它有时会起作用,但尚不清楚(至少对我而言)它在什么条件下起作用,并且没有计划改进对此的支持。所以我必须用另一种方式来做这件事。

于 2009-11-24T19:20:40.397 回答
0

也许这个页面解释了你所追求的代码:http: //blog.4webby.com/posts/view/6/cakephp_models_using_multiple_db_connections

希望有帮助。

西蒙。

于 2009-11-21T03:16:01.257 回答
0

我试图在 cakeapp.com 中重新创建您的结构。查看http://cakeapp.com/sqldesigners/sql/oses上的架构

但是当我尝试在此处自动创建的应用程序中添加设备时:http: //oses.cakeapp.com/cake/oses/devices/add似乎有点不对劲。

您可以使用密码更改数据库结构:“oses”。并让 cakeapp.com 通过单击链接 #3 自动创建应用程序:“Build the CakeApp application based on the settings and the SQL”

也许这对你有帮助?

于 2009-11-24T17:21:16.300 回答