1 model 2 databases

Hi,

I need to manage 2 synchronizes databases in my yii backend.

Each insert,update,delete need to be done in both databases.

I already declare my db in config\main.php like this.


'db'=>array(

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

  'emulatePrepare' => true,

  'username' => 'user',

  'password' => 'user',

  'charset' => 'utf8',

),

'db_backup'=>array(

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

  'emulatePrepare' => true,

  'username' => 'user',

  'password' => 'user',

  'charset' => 'utf8',

  'class' => 'CDbConnection'

),



Somebody have idea where to start, Models overrides, controller, …

I know some topic already speak how to use 2 databases to fetch data, but none speak about override insert,update,delete.

Thank you for help.

I found a solution to insert,delete,update in 2 database in same time.

Simply use MyActiveRecord instead of CActiveRecord.

config/main.php




'components'=>array(

...

		'db'=>array(

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

			'emulatePrepare' => true,

			'username' => 'user',

			'password' => 'user',

			'charset' => 'utf8',

		),

		'db2'=>array(

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

			'emulatePrepare' => true,

			'username' => 'user',

			'password' => 'user',

			'charset' => 'utf8',

            'class'            => 'CDbConnection'          // DO NOT FORGET THIS!

		),

...

)



And my MyActiveRecord.php class




<?php

// protected/components/MyActiveRecord.php

class MyActiveRecord extends CActiveRecord

{

    private static $dbmssql = null;

	protected static $cur_db = 'db';


	public function getDbConnection()

    {

		if (self::$cur_db == 'db')

		{

			self::$cur_db = 'db2';

			return parent::getDbConnection();

		}

		else

		{

			self::$cur_db = 'db';

			return self::getDb2Connection();

		}

	}

	

    public function getDb2Connection()

    {

        if (self::$dbmssql !== null)

            return self::$dbmssql;

        else

        {

            self::$dbmssql = Yii::app()->db2;

            if (self::$dbmssql instanceof CDbConnection)

            {

                self::$dbmssql->setActive(true);

                return self::$dbmssql;

            }

            else

                throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));

        }

    }


	public function deleteByPk($pk,$condition='',$params=array())

	{

		parent::deleteByPk($pk,$condition,$params);

		return parent::deleteByPk($pk,$condition,$params);

	}	


	public function insert($attributes=null)

	{

		parent::insert($attributes);

		return parent::insert($attributes);

	}	

	

	public function updateByPk($pk,$attributes,$condition='',$params=array())

	{

		parent::updateByPk($pk,$attributes,$condition,$params);

		return parent::updateByPk($pk,$attributes,$condition,$params);

	}

}


?>



And what about a master/master replication of databases? Wouldn’t that be easier ? :)

My final objective is synchronize 1 mysql database on local server and 1 mssql database on another server.

That’s why I didn’t choose to option database replication.

Insert/Delete/Update on bold servers.

Select on localhost.