3

必然的に複数のデータベースにまたがるプロジェクトがあります。

1 つのデータベースにはテーブルがあります。

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,                            
                )
        );
}
?>

3 つのテーブルすべてが 'default' または 'otherdb' に存在する場合、Device から OsesPlatform への hasOne または belongsTo 関係の 'conditions' 引数でそれを行うことができ、実際、Platform と OsesPlatform の間の hasOne 関係は正常に機能します。ただし、モデル化する必要があるのは、デバイスとプラットフォームの関係です。

4

3 に答える 3

1

結局のところ、データベース間の HABTM 関係は Cake では公式にサポートされていません (これは #cakephp の markstory から)。動作することもありますが、(少なくとも私には) どのような条件下で動作するかは明確ではなく、これに対するサポートを改善する予定はありません。だから私はこれを別の方法で行う必要があります。

于 2009-11-24T19:20:40.397 に答える
0

Cakeapp.com であなたの構造を再現しようとしました。http://cakeapp.com/sqldesigners/sql/osesでスキーマを見てください。

しかし、ここで自動的に作成されたアプリにデバイスを追加しようとすると: http://oses.cakeapp.com/cake/oses/devices/add少し間違っているようです。

パスワード「oses」でDB構造を変更できます。そして、リンク #3: 「設定と SQL に基づいて CakeApp アプリケーションを構築する」をクリックして、cakeapp.com に自動的にアプリを作成させます。

多分これはあなたを助けますか?

于 2009-11-24T17:21:16.300 に答える
0

おそらく、このページはあなたが求めているコードを説明しています: http://blog.4webby.com/posts/view/6/cakephp_models_using_multiple_db_connections

それが役立つことを願っています。

サイモン。

于 2009-11-21T03:16:01.257 に答える