dongsuoying9059 2014-07-10 12:49
浏览 40
已采纳

yii来自其他数据库的活动记录相关记录

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
}
  • 写回答

1条回答 默认 最新

  • doutang1873 2014-07-11 08:15
    关注

    Okey, I explored documentation, comments and now this problem is solved.

    Tables in one database cannot directly reference tables in another database, and this means that relations don't cross DB boundaries.

    http://www.yiiframework.com/wiki/123/multiple-database-support-in-yii/

    Solution. Lets write setup params for all db connections which module will be use in the future. Example:

    'modules' => array(
        'module_name' => array(
            'db1' => array(
                'class' => 'CDbConnection',
                'connectionString' => 'mysql:host=...;dbname=db1',
                'username' => 'user',
                'password' => 'password',
                'charset' => 'utf8',
            ),
            'db2' => array(
                'class' => 'CDbConnection',
                'connectionString' => 'mysql:host=...;dbname=db2',
                'username' => 'user',
                'password' => 'password',
                'charset' => 'utf8',
            ),
        )
    ),
    
    ...
    

    In module init() method or another logic entry point you need to create objects of CDbConnection class, it's something like that:

    $db1_connection = Yii::createComponent( Yii::app()->controller->module->db1 );
    $db2_connection = Yii::createComponent( Yii::app()->controller->module->db2 );
    

    Then use CDbConnection::createCommand to get needed data from databases.

    // Records from db1.
    $parent_records = $db1_connection
        ->createCommand('SELECT * FROM parent_table')
        ->queryAll();
    
    // Records from db2 as childs of parent record.
    foreach( $parent_records as &$parent_record ) {
        $parent_record['childs'] = $db2_connection
            ->createCommand('SELECT * FROM child_table WHERE parent_id = :parent_id')
            ->bindParam(':parent_id', $parent_record['parent_id'])
            ->queryAll();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀