Where should I put the DAO in Yii2

I’m new to the Yii2 framework. I used to work with Java before, and I followed the DAO concept to create my web applications. I know this is a newbie question, but I read the Yii documentation and it’s not quite clear to me how to design my persistence layer. There is the “Search” class, which you can generate throught Gii. My question is,is the Search class like the DAO class in the Yii framework?

The pattern mainly used in Yii is called Active Record. While in DAO you have an entity and an object that is responsible for persisting it, Active Record is a hybrid of two. It is an entity that can persist itself.

Search class is inherited from Active Record so it’s a kind of it.

If you prefer, you can follow the approach you’ve used to i.e. have a DAO that uses query builder internally (usually named Repository) and clean PHP classes as entities. That is a bit repetitive approach at the start but it may give you extra flexibility later. Beware though that if you’re working in a team with members not familiar with Java, it would be quite unusual for them.

3 Likes

@samdark thank you for the great response and advice! It’s very clear to me now. You guys do a great job!

1 Like