I have a project that necessarily covers several databases.
There are tables in one database:
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)
}
Another database has a table:
CREATE TABLE oses_platforms {
platform_id integer unsigned not null primary key auto_increment,
os varchar(50),
platform_id integer unsigned
}
I created Models for tables and relationships:
<?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,
)
);
}
?>
If all three tables are in "default" or "otherdb", I can do this in the "condition" argument for hasOne or belong to a device-to-OsesPlatform relationship, and in fact the hasOne relationship between Platform and OsesPlatform works fine, However, this relationship is between device and platform that I need to simulate.
source
share