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 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示