Yii 1.1 Use two database connection at same time

I want to use to database connection at the same time. I got two servers having same database. I want to update both the database at the same time i.e On the add/update/delete on my app both the database needs to up updated accordingly. I have added the following in the main.php


        'db2'=>array(

        'connectionString' => 'mysql:host=hostname;dbname=dbanme',

        'emulatePrepare' => true,

        'username' => 'username',

        'password' => 'password',

        'charset' => 'utf8',

        'tablePrefix' => 'tbl_',

        'enableProfiling'=>true,

        'enableParamLogging'=>true,

        'class' => 'CDbConnection'

And then in my controller in the Add action :


public function actionCreate()

    {

       $model=new TestTransaction;


       if(isset($_POST['TestTransaction']))

       {

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

            $latin1DbConnection     = Yii::app()->db2;

            $model->attributes      = $_POST['TestTransaction'];

            if($model->save()) {

                Yii::app()->setComponent("db",$latin1DbConnection);

                $model->save();

                Yii::app()->setComponent("db",$originalDbConnection);

                $this->redirect(array('view','id'=>$model->id));    

            }

        }


        $this->render('create',array(

            'model'=>$model,

        ));

    }

But this is not working. Please help me out.

In your TestTransaction model you can override getDBConnection method.

Here is good example http://www.yiiframework.com/wiki/123/multiple-database-support-in-yii/