What goes into ActiveRecord derived classes?

I have create a lot of source code but not designed it very well as it seems. Now I’m wondering how should I design it in general? In particular I’d like to know what should be part of classes that extend ActiveRecord.

E.g. I have lots of read-only methods that return an ActiveQuery with some filtering. E.g. getLatestEntries(), getHigestRatestEntries(), getWhatsoeverEntries() and such things. Is it better to have such queries in seperated classes which are derived from ActiveQuery? What would be the advantage? And sometimes I use the class for other queries that return some value instead of ActiveRecord objects (e.g. getNumberOfTodayEntries()) - I use them here since they operate on the same table as the class does and I don’t know where to put them else.

And also I have some updating methods here that do more or less complex things with the record object.

If more than one record object is updated/created/deleted I put the code into an transaction, but placed in the controller - which makes the controller fat…

I somehow know that ActiveRecord classes are (or ought to be) be part of the data layer. But which kind of classes should I have then as well? I neither want to have fat controllers nor fat ActiveRecord derived classes. I suspect there must be some typical kind of classes or a typical architecture for most web application that are beyond MVC and ActiveRecord patterns.

Would it be a solution to have many ActiveRecord classes that operate on the same table? Is this an approach?

How do you do that? What can you recommend? Are there any examples? In the Yii guide there are many examples, but those are simple and the controllers directly use a single ActiveRecord object.

I’m confused and don’t know how to organize my code. The code is getting more and more a mess!


Sounds that you’re ready to start building domain layer. A set of classes not tied to framework itself that contain more or less complex logic for calculations or saving complex things or any other things that don’t fit into AR model itself.