Where to put this requirement? another suggestion and doubt

Hi all!

If I have a requirement, like a method that retrieves an specif model (or many) that needs to be used by many actions from many controllers.

where should I put this method?

For instance, I don’t agree to put “findRecentComments” method inside Comment ActiveRecord.


If a instantiate a new comment, this object should not know nothing about recent comments.

Because, it is a simple new entry from the database and it isn’t persisted yet inside it.

Even if it was, it should know just about itself, it is unique, it is an object that should know, retrive, manage ALL, but just about itself or another related objects

there should be a place who can manipulate these scenarios.

I can put a method like this inside the controller that managers the model that I’m needing.

So, how can I use, in Yii, a method of a controller if Im in another action from another controller?

more, in heavy applications i could have too many actions for a controller (model) and it would make the code less unreadble, and I’d have to do a refactoring because i could have a class with hundreds of code lines. (see Marting Flower)

that´s an observation that we can put in the documentation.

This method really belongs into a model hence AR in your case if you wanna stick to MVC.

Usually you would use a static method since its not object specific information.

In yii though I would just write a normal method which you call like that:

$recentComments = Comment:: model()->findRecentComments();

Thats the easiest and cleanest way in my opinion.

You probably won’t call findRecentComments() from a newly created Comment model, will you?

You don’t even have to implement this method to if you want to keep models neat, since Comment::model()->findAll(array(‘limit’=>$max,‘condition’=>‘status=approved’)); does the same trick.

Maybe you can use Behaviors (http://www.yiiframework.com/doc/guide/basics.component#component-behavior) to add these requirements in your controllers.

For the findRecentComments() example, you can also use named scopes.

it could be easy and clean (i igree) but if you have many methods like this, your class code may increase a lot and you would have to refactor this.

i mean, a method ( or a fragment code) that needs to be used for diferents controllers.

got it?


that’s ok for Behaviors, and about named scopes?

can u explain that?

Here’s an example from the guide:

class Post extends CActiveRecord



    public function scopes()


        return array(





                'order'=>'createTime DESC',






You can get the most recent posts using:


You can’t reuse it in other controllers or models. The findRecentComments method was a bad example about what you want to do.

I think something is wrong if you have a controller with many actions. It’s almost certain that this actions can be divided in other controllers. If you need to access this in many places, think about behaviors, helpers or components. There’s no documentation about helpers on yii, but you can user CFileHelper class as an example. It’s a simples class with only static methods.

think about behaviors, helpers or components

exactly where the idea is.

i just to wanted to know what other ones thought about this scenario.

exchange ideas, once the documentation still needs improving.