0

I use yii framework that implements Active Record pattern as ORM base. It has CActiveRecord class that is a table wrapper class with attributes reflecting table columns. So each object of this class represents a database row.

Wiki says about Active Record pattern:

Active record is an approach to accessing data in a database

and

A database table or view is wrapped into a class. Thus, an object instance is tied to a single row in the table.

So far so good.

But where should I put complex raw sql query that retrieves statistics data for example?

And, more generally, where should I put methods that retrieve some data that can not be an active record object (like data retrieved with aggregation queries) or if I knowingly do not want to retrieve an object but an array instead for example?

4 Answers 4

1

And for complex queries you can always use DAO if you want: http://www.yiiframework.com/doc/guide/1.1/en/database.dao

But in most cases, CDbCriteria will fit your needs, you can read more about it here: http://www.larryullman.com/2013/07/24/using-cdbcriteria-in-the-yii-framework/

3
  • But where should those methods that use dao reside? In model class extending CActiveRecord? Sep 22, 2013 at 22:14
  • 1
    In your controller or model, usually in your model.
    – davey
    Sep 24, 2013 at 7:41
  • yeah, that's the approach I came to as well. Though the following separation might make sense: there is an ActiveRecord class where all the db query methods reside, and ActiveRecord class that extends the former, where all the business logic methods reside. I guess that's a good separation of concerns that can be achieved really easy. All other cases when these classes are independent don't seem easy and actually fall out of ActiveRecord pattern implementation. Sep 24, 2013 at 13:38
0

There are many possibilities depending on what you want. Yii has relations to access related objects and one of the relation type is statistical relation, check this link: http://www.yiiframework.com/doc/guide/1.1/en/database.arr#statistical-query

You may also use naming scopes to filter some of your results and then call for example count function to retrieve number of filtered results (this will be done by sending select count(*) ... to db server rather than fetching all entries, so it's very convenient). Check this for named scopes: http://www.yiiframework.com/doc/guide/1.1/en/database.ar#named-scopes

If statistical data is related to your model, for example total spending by some client (although this could be done easily using statistical relation), you can add public function directly to your model class, such as

public function getTotalSpending() {
    return 0; // or whatever you need to calculate here
}

Finally it is not considered a good practice to map your business logic directly to tables. Instead create your models by subclassing CModel or CFormModel classes and add public methods that retrieve / modify data (possibly using other models that do inherit CActiveRecord class).

2
  • But Active Record pattern implies writing business logic in active record classes. It just comes naturally as the result of most db queries is active record class(-es). Sep 22, 2013 at 22:17
  • Anyway, there should be a separation of database layer and business logic layer -- otherwise there will be a mess in active record classes. One should use Data Mapper pattern instead. Personally I consider Active Record an anti-pattern, it just brings mess and chaos in project that is just a little bigger than my cat's homepage. Sep 22, 2013 at 22:23
0

Use CArrayDataProvide

Elements in the raw data array may be either objects (e.g. model objects) or associative arrays (e.g. query results of DAO). Make sure to set the keyField property to the name of the field that uniquely identifies a data record or false if you do not have such a field.

source: http://www.yiiframework.com/doc/api/1.1/CArrayDataProvider

1
  • Thanks, but how does it correlate with my question? Sep 22, 2013 at 22:25
0

Do not ever use Active Record pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.