trevis
(Theredcheese)
1
Currently I'm using:
$this->connection = new CDbConnection(Yii::app()->params['db_dsn'], Yii::app()->params['db_user'], Yii::app()->params['db_pass']);
$this->connection->active = true;
to connect to a second database that is separate from the main application database. I have a few questions about it:
-
Can I use active record and somehow tell it to use this specific connection for one model instance, but use the default connection elsewhere?
-
If not, what is the correct way to generate update/insert/query sql with this connection? Should I be constructing the sql by hand?
Examples are appreciated, thanks.
will
(Phpwindcn)
2
I think you can just define multiple DB components in config main.php:
say we have a master db and a slave db, we can define them like this
'db_master'=>array(
'connectionString'=>'mysql:host=localhost;dbname=db1',
'username'=>'root',
'password'=>'password',
),
'db_slave'=>array(
'connectionString'=>'mysql:host=anotherhost;dbname=db2',
'username'=>'root2',
'password'=>'password2',
),
so that you can use
Yii::app()->db_master
to insert, update, delete
while use Yii::app()->db_slave for query purpose only.
I haven't tested this approach yet, so please correct me if these is any mistake.
jonah
(Poppitypop)
3
Can you configure models to use a certain connection? if not, a ticket should be created for this.
qiang
(Qiang Xue)
4
To use multiple DBs for AR, you need to override its getDbConnection().
system
(system)
5
Can someone provide example of overriding getDbConnection in model
megabr
(Megabr)
6
please
Can someone provide example of overriding getDbConnection in model
qiang
(Qiang Xue)
7
Please check the code of CActiveRecord::getDbConnection() and you will see how.
trevis
(Theredcheese)
8
Thanks, this is working great.
ppy
(Popeye)
9
Please, example or cook book.
SJFriedl
(Steve)
10