separate database for master tables

Hi All,

I am developing a system for a school. It consists of some modules (3-5 modules) that is each developed in separate yii project. I am developing the second module now. I found out that some tables are duplicates from the first module. In order to reduce the maintenance, is that possible to use same tables? Hence, updating a table from first module will be reflected on the second module.

I am thinking of using multiple connection to the database. I will have one database to store only master tables. While module specific tables will be stored on the other database.

While, it is not a best practice, I am not really care if the code (model/view/controller) should be copy paste from one module to another. Best, if it is in one place, for the sake of maintenance.

Any help on this?

Cheers,

Daniel

What’s the reason for using the modules in different yii applications?

If you want to work with multiple databases you don’t have to create separate projects.

You only have to config different connections in you config/main.php




array(

    ......

    'components'=>array(

        ......

        'db'=>array(

            'class'=>'CDbConnection',

            'connectionString'=>'mysql:host=localhost;dbname=testdb',

            .... 

        ),


        ......

        'masterDb'=>array(

            'class'=>'CDbConnection',

            'connectionString'=>'mysql:host=localhost;dbname=masterdb',

            .... 

        ),

    ),

)




Now you can access the db’s like





$connection1=Yii::app()->db;  

$command=$connection1->createCommand($sql);


 ....

$connection2=Yii::app()->masterDb;  

$command=$connection2->createCommand($sql);

 ....




Hi Joblo,

Thanks for your help.

What I mean by module is not the yii module, but more to development phase and different location of the deployed server. Master tables will stay together with the module for educational. However, there will be finance module that deployed on dfferent server but must use master table on the educational server.

My master tables, like students, units and subjects need to be consistent, mean changes made by the operator from one module say finance department need to be seen by the educational department and vice versa.

I like your idea, but I do not want to use the createCommands. I prefer to use CActiveRecord. Is that possible? How to differentiate that a model need to use db or masterDb connection?

Cheers,

Daniel

Override CActiveRecord::getDbConnection() in the model class.

Any example on the content of the getDbConnection? I am still not quite understand.

Thanks in advance.


return $this->getComponent('masterDb');

write above code, instead of


self::$db=Yii::app()->getDb();

in the method that overrides CActiveRecord::getDbConnection()

For details, check CActiveRecord::getDbConnection()