adinugro
(Adinugro)
March 15, 2013, 4:26am
1
Hi,
I am having problem in handling requirements of my project. I have one table called transaction. However, it asked me to be able to store the transaction on 2 different database. If the code is 0 then, it will save on the first database, while if the code is 1, it should be stored on the second database.
Can we do this in yii?
Regards,
Daniel
satan666
(Nirmal9847)
March 15, 2013, 9:25am
2
Daniel:
Hi,
I am having problem in handling requirements of my project. I have one table called transaction. However, it asked me to be able to store the transaction on 2 different database. If the code is 0 then, it will save on the first database, while if the code is 1, it should be stored on the second database.
Can we do this in yii?
Regards,
Daniel
http://www.yiiframework.com/wiki/123/multiple-database-support-in-yii/
seenivasan
(Chellamnivas)
March 15, 2013, 4:03pm
3
Dear Friend
You can attach a behavior to main application.
In the behavior method we can create a new database connection , based on the condition.
main.php
........other properties....
'behaviors'=>array(
'class'=>'application.components.applicationBehavior',
),
'components'=>array(
......other component declaration...
//DEFAULT CONNECTION...
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=databaseRoutine',
'emulatePrepare' => true,
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
),
components/applicationBehavior.php
class applicationBehavior extends CBehavior
{ private $_owner;
public function events()
{
return array(
'onBeginRequest'=>'changeDb',
);
}
public function changeDb()
{
$owner=$this->_owner=$this->getOwner();
//PUT THE CONDITION HERE...
if($code===1)
{
$owner->db->setActive(FALSE);
$owner->setComponent('db',new CDbConnection("mysql:host=localhost;dbname=newDatabaseName",'username','password'));
$owner->db->setActive(TRUE);
}
}
}
?>
I hope this will help you a bit.
Regards.