2009-11-19 30 views
3

我有一個必須跨越多個數據庫的項目。CakePHP中主鍵以外的字段上的多個數據庫關係

一個數據庫有表:

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'中,我可以在hasOne或b的'conditions'參數中執行elongs從Device到OsesPlatform的關係,實際上Platform和OsesPlatform之間的hasOne關係工作正常。但是,我需要對設備和平臺之間的關係進行建模。

回答

1

事實證明,跨數據庫的HABTM關係沒有在Cake中正式支持(這來自#cakephp上的markstory)。雖然它有時會起作用,但在什麼條件下它不會工作,至少對我而言並不清楚,也沒有計劃改進對此的支持。所以我必須以另一種方式做到這一點。

0

我試圖在cakeapp.com重新創建你的結構。 看看http://cakeapp.com/sqldesigners/sql/oses

但當我嘗試在這裏自動創建的應用程序添加設備:http://oses.cakeapp.com/cake/oses/devices/add它似乎有點錯了。

您可以使用密碼「oses」更改數據庫結構。並讓cakeapp.com通過單擊鏈接#3自動創建應用程序:「基於設置和SQL構建CakeApp應用程序」

也許這對您有所幫助?

+1

那麼,你已經添加了另一個領域的「設備」,我不能做。我意識到我沒有提到這一點。無論如何,加入需要在devices.os上,而不是devices.osesplatform_id。 – 2009-11-24 19:18:04

相關問題