Reputation: 31
How to make two or more database connections in Yii mongodbsuite?
I added 2 DB components in main.php:
'components' => array(
'mongodb' => array(
'class' => 'common\extensions\MongoDB',
'connectionString' => 'mongodb://localhost:27017/table1',
'dbName' => 'table1',
'fsyncFlag' => false,
'persistentConnection' => 'x',
'replicaSet' => false,
'safeFlag' => true,
'useCursor' => false,
),
'mongodb2' => array(
'class' => 'common\extensions\MongoDB',
'connectionString' => 'mongodb://localhost:27017/table2',
'dbName' => 'table2',
'fsyncFlag' => false,
'persistentConnection' => 'x',
'replicaSet' => false,
'safeFlag' => true,
'useCursor' => false,
),
)
But how to use mongodb2
in query (ex: ->findByAttributes()) i don't know.
Please provide some example queries using the mongodb2
connection above.
Upvotes: 1
Views: 898
Reputation: 31
I just add 'mongodb2' => array to 'components' => array and add to models (ex: User) who extends EMongoDocument:
public function getMongoDBComponent() {return Yii::app()->mongodb2;}
And it is work!
Upvotes: 2
Reputation: 2977
Have a look at Multiple db support in Yii.
Basically you have to modify your active record class by overriding the getDbConnection()
methods. (In the given link you have a better example using an intermediate inheritance layer)
Upvotes: 0