Counter behavior?


Is in Yii behavior or any like something in cakePHP, which auto update counter record in HAS_MANY record?

I have a lot relation in my DB, and in a lot of cases I need fast access to counter how much related records has my record.

I want to always when user will create or delete related records, counter fields in related records will be auto updated.

In cakePHP I was using so simple mechanism. I had only to write in model something like ‘countercache’ and this was enough.

Is in Yii simple,fast way to get it?

As far as I know, there is nothing of similar.

You can create a stat relation for retrive the number of related record. Each time will be a new query, so it will be always updated.

how to?

Take a look at the documenetation of CStatRelation.

It has the same interface of a relation, and you can build the query you need.

Then on each model you can call $model->statRelationName and retrive what you need.