I have model named SuperModel in one module/models folder. And related ChildModel1, ChildModel2 in another module/models folder.
SuperModel data record from database1 must contain related records Child1 and Child2, which placed in another databases.
Is there way to get record using relations from few databases at the same time via yii ActiveRecord relationship mechanism? Something like that:
$super_record = SuperRecordModel::model()->with(
'RecordFromDB_A',
'RecordFromDB_B'
)->findAll( $criteria );
Or I need use something like:
// From first DB...
$super_record = SuperRecordModel::model()->findAll();
// Separately from another DB A...
$super_record->ChildsA = ChildsAModel::model()->findAll(
't.parent_id = :super_record_id'
);
// Separately from another DB B...
$super_record->ChildsB = ChildsBModel::model()->findAll(
't.parent_id = :super_record_id'
);
How is right?
UPDATE: I can't use yii active record relationship in multi-database select actions... How can I switch between databases within active record method? Example:
$catalogs = $this->findAll();
// Set new database connection for this context..
// $this->setDbConnection( yii::app()->db2 );
foreach( $catalogs as &$catalog ) {
$catalog->service = Services::model()->find(
't.catalog_id = :catalog_id', ... );
// this gives error: table services cannot be found in the database
}