Yii Framework Forum: working with models that reference different databases - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

working with models that reference different databases Rate Topic: -----

#1 User is offline   edwaa 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 17-May 09
  • Location:Seattle, WA

Posted 11 May 2010 - 12:17 AM

I'm about to embark on an application that will use models that reference different databases. If anyone else has some suggestions on how to do this cleanly in Yii I would be very appreciative. Also, I think it is a topic that should be covered in the Yii documentation.

To clarify the data model/database relationship:

model: alpha => database: db1, table: alpha
model: beta => database: db2, table: beta
0

#2 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 11 May 2010 - 09:06 PM

Read this page to configure a database connection.
The default database component is 'db', but you can configure different databases by copying that code to a component named 'db2' (which may hold your beta table).

in your beta model override the getDbConnection() method to return the 'db2' component.
0

#3 User is offline   Spyros 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 461
  • Joined: 14-April 09
  • Location:Athens - Greece

Posted 11 May 2010 - 10:49 PM

Also if your databases are on the same machine you could override the tableName function to return database.tableName instead of just tableName
0

#4 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,738
  • Joined: 17-January 09
  • Location:Russia

Posted 12 May 2010 - 05:04 AM

Config:

'components'=>array(
    //…
    'db'=>array(
        'class'=>'system.db.CDbConnection',
        'connectionString'=>'mysql:host=localhost;dbname=db1',
        'username'=>'root',
        'password'=>'',
        'charset'=>'utf8',
    ),
 
    'db2'=>array(
        'class'=>'system.db.CDbConnection',
        'connectionString'=>'mysql:host=localhost;dbname=db2',
        'username'=>'root',
        'password'=>'',
        'charset'=>'utf8',
    ),
    //…
),


SQL:
/* Post table */
DROP TABLE IF EXISTS `post`;
CREATE TABLE IF NOT EXISTS `post` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(255) NOT NULL,
  `text` TEXT NOT NULL,
  PRIMARY KEY  (`id`)
);

/* Comment table */
DROP TABLE IF EXISTS `comment`;
CREATE TABLE IF NOT EXISTS `comment` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `text` TEXT NOT NULL,
  `postId` INT(10) UNSIGNED NOT NULL,
  PRIMARY KEY  (`id`)
);


Post model is a standard one. Comment model looks like this:

class Comment extends CActiveRecord {
    //…

    public function getDbConnection(){
        return Yii::app()->db2;
    }
 
    public function tableName(){
         return 'db2.comment';
    }  
 
    //…
}


Then you can do:
Post::model()->with('comments')->findAll();

Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users