PDO, DAO, Query Builder and Active Record

Hello everybody,

I’m trying to understand the Db subsystem of Yii.

I have problems about the modules used: PDO, DAO, Query Builder (QB) and Active Record (AR).

I don’t understand how these modules interact each other.

Principally I will use AR to query the DB but when I need to do more complex queries I need to use QB, isn’t it?

But what about PDO and DAO?

Are they used to make uniform the DB connection and avoid problems when I change DB?

Why we use two modules (PDO and DAO) to have DB independence?

Both QB and AR use PDO+DAO?

Maybe this is not very important question but I like to understand better.



PDO is the abstraction level. It allows one use a standard library that will work in various databases. From the php page

The other three are different implementations built on top of PDO. You can read about them here:


Generally, you can stick with active record for most cases. Once you start needing more performance, you can write sql queries directly using DAO/QB.

Yep. Write in AR first if you can, then switch to DAO for slow queries as there is less overhead.

Of course, in practice you will encounter many queries you need to write that are very difficult to do using AR. I think when you start getting into anything beyond simple relational retrievals or simple aggregate stat queries you’ll be switching to query builder / DAO straight SQL.