Yii Framework Forum: Using AR with DAO - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Using AR with DAO Rate Topic: ***** 1 Votes

#1 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 16 July 2010 - 06:54 PM

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;

0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 17 July 2010 - 04:06 AM

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();

1

#3 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 17 July 2010 - 06:31 AM

Thanks Mike! This works fine.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users