3

I would like to connect to a second database with Yii at runtime. The database name would come from a database table after the user to login.

I saw in a tutorial I should do this:

$db2 = Yii::createComponent(array(
    'class' => 'EMongoClient',
    'server' => 'mongodb://127.0.0.1:27017',
    'db' => $emp['database']
));

Yii::app()->setComponent('db2',$db2);

But in my controler when I access Yii::app()->db2 get the error:

Property "CWebApplication.db2" is not defined

What am I doing wrong?

Leonardo Delfino
  • 1,488
  • 10
  • 20

3 Answers3

3

The following works for me:

Yii::app()->mongodb->setActive(false);
Yii::app()->mongodb->setServer('mongodb://localhost:27017');
Yii::app()->mongodb->setDb('db1');
Yii::app()->mongodb->setActive(true);
1

UPDATED: Try, instead instance, pass configurations:

Yii::app()->setComponent( 'db2', array(
                                      'class' => 'EMongoClient',
                                      'server' => 'mongodb://127.0.0.1:27017',
                                      'db' => $emp['database']
                                  )
);

Or, you may create special index on params in configurations, such as:

  ...
  'params' => array(
         'db2' => null,
     ),

And the use Yii::app()->params['db2'] = $db2

CreatoR
  • 1,654
  • 10
  • 14
  • when I try to do this, I get the following error: `Missing argument 2 for CModule::setComponent()` – Leonardo Delfino Oct 17 '13 at 14:29
  • My problem is not with the creation of the component. Soon after creating if I access `Yii::app()->db2` its works, but when I try to access via another model or controller I get the error – Leonardo Delfino Oct 17 '13 at 14:32
  • Ok, try Yii::app()->setComponents( array('db2'=>$db2)); (with "s"). Or try Yii::app()->setComponent('db2', ) – CreatoR Oct 17 '13 at 14:32
  • Interesting. Another thought: reserv name db2 in main configuration and try to change it instanse. If not working, than you may create special key in 'params' and change it... try please – CreatoR Oct 17 '13 at 14:37
  • when i try to access `Yii::app()->params['db2']` its returns null.. I am trying to change the parameters inside a module, you know if yii have any restrictions in this regard? – Leonardo Delfino Oct 17 '13 at 16:47
  • As I understand, in module Yii::app()-> it is local module's enviroment. So, inside the module you can create component by ->setComponent('db2',... - it creates component db2 in the module. Then, I think, you can access to it from main app using Yii::app()->getModule($moduleName)->db2. Try it! – CreatoR Oct 17 '13 at 18:48
0

From this comment:

My problem is not with the creation of the component. Soon after creating if I access Yii::app()->db2 its works, but when I try to access via another model or controller I get the error

I think you are setting this component only once somewhere, and then making subsequent requests to different controllers.

You need to put the code, somewhere it is being called EVERYTIME, on every Request. thats how PHP works, there is no "global application state"

by default Yii comes with protected/components/controller.php has base controller for the rest of the app.

my suggestion would be to put your code on the init() method of that controller, so that it always gets called.

You mentioned the database name comes from a table once the user logs in, so you need to save that value in the session, in other to be able to access it in the other requests:

<?php

// After login in
Yii::app()->user->setState('db_name', $db_name);

// in protected/components/controller.php
public function init()
{
    if (!Yii::app()->user->isGuest) {
        $db2 = Yii::createComponent(array(
            'class' => 'EMongoClient',
            'server' => 'mongodb://127.0.0.1:27017',
            'db' => Yii::app()->user->getState('db_name')
        ));

        Yii::app()->setComponent('db2',$db2);
    }
}

Hope it helps, I am assuming many things here :)

Asgaroth
  • 4,274
  • 3
  • 20
  • 36