Trouble working with multiable databases

I have extended CActiveRecord to allow certain models to use db2 by extending my alternative active record instead of CActiveRecord. Which works fine as long as in the controller I am only working with a model from db or db2, not both at the same time.

In config/main.php I have created connections to two databases:




                'db'=>array(

                        'class'=>'CDbConnection',

                        'connectionString' => 'mysql:host=***;dbname=***',

                        'emulatePrepare' => true,

                        'username' => '***',

                        'password' => '***',

                        'charset' => 'utf8',

                ),


                'db2'=>array(

                        'class' => 'CDbConnection',

                        'connectionString' => 'mysql:host=***;dbname=***',

                        'emulatePrepare' => true,

                        'username' => '***',

                        'password' => '***',

                        'charset' => 'utf8',

                ),



In the site controller I have a function called actionIndex() to render the index.php page.




public function actionIndex()

{

  $model=new CustomerForm;

  $dataProvider=new CActiveDataProvider('data_products', array('criteria'=>array('condition'=>'dataRequestType=0') ) );


  $a = ProgramCount::model()->count();

  echo "<pre>";

  print_r($a);


  if(isset($_POST['CustomerForm']))

  {

    $model->attributes=$_POST['CustomerForm'];

    if($model->validate())

    {

      //Do something

    }

  }

  

  $this->render( 'index',array('dataProvider'=>$dataProvider, 'model'=>$model) );

}



CustomerForm is just a form model that extends CFormModel so I don’t think that is my problem. The data_products model called in CActiveDataProvider uses my alternate CActiveRecord to get access to ‘db2’, and the ProgramCount model just extends CActiveRecord to get access to the first database called ‘db’.

When I try to run the code as is I get an error saying "The table "programCount" for active record class "ProgramCount" cannot be found in the database." The table the model ProgramCount is trying to access is called programCount.

However if I comment out the line:

“$dataProvider=new CActiveDataProvider(‘data_products’, array(‘criteria’=>array(‘condition’=>‘dataRequestType=0’) ) );”

I do get information returned from "$a = ProgramCount::model()->count();" as expected.

I think this is because CActiveDataProvider sets the current database being used as the database that data_products uses, and not the database that the ProgramCount model uses.

Any help or advice would be greatly appreciated.

Try overriding getDbConnection() for all involved AR classes. This works for me.

The default implementation of CActiveRecord::getDbConnection() does check if the static property $db is set. If that’s the case it will be used. Now if you extend one AR from another, the static $db property could be set with the db component from another AR.

Thank you so much that worked perfectly.