Location of shared model code

I’m not sure where I should be putting code that access a model but is shared by more than one controller

EG updateByPk.

At the moment I have it in the model as a public method. However, this is not ideal as it then creates a second version of the model to call the update ( modelname::model()->updateByPk )

My question is, where do I put this code. Or am I going about it wrong? Inserts have no overhead because I can just use $this->param to set them up and $this->save(). This makes me think I am missing something.

I’m being dumb.


is equivalent to


… but I’m still creating two model objects because

$data = $this->find(...)

assigns a new model to $data. $this->myAttribute does not return the attribute that is found through $data->myAttribute.

So my question still stands.

I could use a static method, but then it won’t have access to any private helper methods - leading to bad class design.

I don’t quite understand your issue. Could you state a use case please?

On the surface it sounds like you need to define a base model class for all of your models to extend. Then you have a common ancestor.

Sorry for slow response. I’ve been away.

I have some models that are accessed by multiple controllers. For example, some sites like stack overflow have a point system; where each user gets points for activity on the site, which in turn enables certain features of the site. The models involved in this points system need to be accessed by many controllers, and often in the same way. This requires an extra layer, so that code is not duplicated in the controller classes. However the model is not the best place for this, as it extends Active Record and so comes with a lot of overhead.

I could create a library in the components folder. But this would be confusing as the classes would be directly related to the model classes.

My solution at present is to create two classes in the model folder. one named normally. The other named the same but with ‘Access’ on the end . This way the code is in a logical location and I don’t have the overhead of instantiating Active Record classes.

What I want to know is if there is a standard here I am not familiar with? Am I missing something?

Why don’t you create a base controller with common methods or a behavior attached to some controllers?

I have a base controller - but that is loaded with every controller, I’d like to keep it as light as possible.

How do I create a behavior attached to some controllers?

You can find info in the guide & cookbook. In short words:

class MyController extends Controller


    public function behaviors()


        return array(







class SomeBehavior extends CBehavior


    public function findSomeModels()


        return SomeModel::model()->findAll();



Now MyController has a method findSomeModels(). You can attach this behavior to many controllers.

Thanks for your help Andy

Am I right in thinking that behaviours are lazy loaded?

Docs say: