Cache relations


return array(

  'author'=>array(self::BELONGS_TO, 'User', 'user_id','joinType'=>'INNER JOIN'),

  'comments'=>array(self::HAS_MANY, 'Comment', 'post_id',



I have post model with 2 relations author and comments i’m planing to cache data.

And if ill cache the result of


and when there will be new comment ill have to clear all the cahche (author and post)

so how i should do ?

get all data in seperate sql? Or there is something clever ? :)

and by the way where should be cashe logic ? in controller or model ?

thank you for your time.

CActiveRecord::getRelated() is an underlying method that is called if you invoke a known relation.

If you explicitly use this method to retrieve comments, you could also define a parameter indicating enforced refresh. See API for precise signature.

Thanks for info peestaa,

actually it wast very helpful for my brains :D

So i have more questions,

I’m looking for right way to cache data but with no results.

First question in my mind is if i will cache comments with author information and the author have 1000 comments

and when he will change information like name or etc. i will have to clear 1000 cached comments pages and i think that it is not cool… right?

maybe someone knows articles about how to cache data in the right way ?

you mentioned CActiveRecord::getRelated()

so if understand you correctly i should do smt like this in postController?


if ($comments===false) {

  $comments = $post->getRelated('comments');



or i get it wrong?

You used it very well, but I can’t confirm (maybe you should add boolean value to getRelated to enforce reloading).

Talking about cache dependency, yes, it is not cool to purge cache only to regenerate them with a new nickname. But how often do your users change their name? If it happens so often, you might consider dynamic fragment content within cached pages, or disallowing name changes at all.

Thank you for your time i own you a bottle of Beer :)