Using AR with DAO

Hi to all!

I usually have the pretty simple DB queries in my code and use ActiveRecords for implementing. But now I have a quite complex query and I think I should handle this via DAO. But I don’t understand how right to connect to the database with these both methods?

Assume I executed two queries with AR, then I need to execute one with DAO and, again, one more with AR. The overall result is four queries. But how many connections to the database is creating? I’m not sure if there are three connections in this case.

Anyway, do you know the right way for working with AR and DAO together? May be there is no necessity to establish new connection each time where I wanna use DAO:


$connection=new CDbConnection(Yii::app()->db->connectionString, Yii::app()->db->username, Yii::app()->db->password);

$connection->active=true;

About the number of connections: Both AR and DAO can use the same CDbConnection you configured in main.php. By default this is the "db" component. So only one connection will be established.

To create a command with DAO, simply do:


$command=Yii::app()->db->createCommand('SELECT...');

$result=$command->queryAll();



Thanks Mike! This works fine.