Use CDbConnection inside another component

Hi people,

I’ve just joined the forum. For the last few days, I’ve been looking into Yii and I’m starting my first web application with it, so I guess it’s normal to have a few questions. :)

I'm trying to modify the [tt]authenticate()[/tt] method of [tt]UserIdentity[/tt] of the default web application (as the yiic tool sets it up). I want to use the [tt]CDbConnection[/tt] object that is set up as an application component (I have modified [tt]main.php[/tt] to use a database connection), but I see that there is no default connection object.

Any help?

Thank you.

Never mind. I found the answer.

The database connection object is found by calling

Yii::app()->getDb()

Learning… :)

if you have in your config:

array(

'components'=>array(

'db'=>array(…

)

then you can then access the DB connection via Yii::app()->db which is already activated automatically, unless you explictly configure CDbConnection::autoConnect to be false. Using

this approach, the single DB connection can be shared in multiple places in our code.

see: http://www.yiiframew…de/database.dao

Yes, that’s what I did. It works fine now. Thanks for the reply. :)