I have a project that necessarily spans several databases.
One database has tables:
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)
}
The other database has the table:
CREATE TABLE oses_platforms {
platform_id integer unsigned not null primary key auto_increment,
os varchar(50),
platform_id integer unsigned
}
I've created Models for the tables and the relationship:
<?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 resided in 'default' or 'otherdb', I could do it in the 'conditions' argument to a hasOne or belongsTo relationship from Device to OsesPlatform, and in fact the hasOne relationship between Platform and OsesPlatform works fine. However, it's the relationship between Device and Platform that I need to model.