Double PDO connection or simply duplicated log ??

Hi @ all !

In my web app, I have to instantiate a class which extends CDbConnection :


$db = new DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect);

class DBLayer extends CDbConnection

{

...

and @ a further point of my code I have to call a model that extends CActiveRecord :


$tags=Tag::model()->findTagWeights($this->maxTags);

class Tag extends CActiveRecord

On the Yii log I can read “Opening DB connection” 2 times so I’m wondering if there is 2 CDbConnection instances used or if it is only a duplicated log phrase.

[center][/center]

Yes there are two connections. This is expected since you auto-open one by default when initiating a CDbConnection (see here). The other one gets opened when using the active-record (which will use the "db" application component - another CDbConnection).

If you need a custom (extended) version of CDbConnection, then you should define the correct class in the config:




'components' => array(

   ...

   'db' => array(

      'class' => 'application.components.DBLayer',

      ...

   ),

   ...

),



Also you don’t have to initiate a new instance in your example. Simply do:




$db = Yii::app()->db;



or use it directly:




Yii::app()->db->createCommand(...);



ActiveRecord will use the same connection.

Thanks.

I will test this soon.

works great, thanks !