Difference between #1 and #2 of
Multiple-database support in Yii

Revision #2 has been created by Steve Friedl on Dec 21, 2010, 12:58:20 AM with the memo:

Fixed broken link to other wiki article
« previous (#1) next (#3) »


Title unchanged

Multiple-database support in Yii

Category unchanged


Yii version unchanged

Tags unchanged


Content changed

Every model defined in `protected/models/*.php` includes `GetDbConnection()` in the base class, and it returns a handle to the DB connection object for the primary database. We need to override this method in the models representing the advertising database to return the second DB connection.

Though it's possible to do this in the model definition file itself, this doesn't scale well as it would duplicate a lot of code if more than one model lives in the Advertising database. Better is to use a custom wrapper class to `CActiveRecord` where this can be centralized.

The notion of custom wrapper classes is described in
<a href=[this wiki article](http://www.yiiframework.com/wiki/121/extending-common-classes-to-allow-better-customization/">this wiki article</a>), and we'll assume that you've created a `protected/components/MyActiveRecord.php` file, and teachaught all of your model files to extend `MyActiveRecord` rather than `CActiveRecord`.

// protected/components/MyActiveRecord.php

class MyActiveRecord extends CActiveRecord {
20 0
Viewed: 114 322 times
Version: 1.1
Category: How-tos
Tags: database
Written by: Steve Friedl
Last updated by: phazei
Created on: Dec 21, 2010
Last updated: 5 years ago
Update Article


View all history