Where To Store Model Methods


I’m wondering where to store buisness logic in Yii MVC structure.

For example i want to have an "interface" (not php term) to get an user.

Should i do a static method in my User model ? and then call "self::model()->findByPk($userId);" ?

This approach will make this file huge, it contains validation data, relations, labels, custom methods like "getFullName" that concat first and last names, and then added to this buisness logics methods ?

I’m just asking for a separated Model in fact, where all access to Database are made.

in MVC,

C is your interface where V sees through and M represents your data which could be asked from C and presents to V for user to enjoy from browser.

in Yii world, all the hard work accessing data and managing relationships have been done via ActiveRecord model which you inherit from when you build your own data model.

if you check any examples of model (check yii demo), you will notice that in model you deal with more ‘definitions’ rather than actual code. you define validation rules, you define relations etc. all the hard work and generic repeating work has been done by yii activerecord model.

so thin C, fat M, clean V, if you feel M is too fat there must be something wrong imho or if you have to, you could manage them in separated files combine using include or Yii::import at runtime.

Well, i think Model is fat because the generated file from the main table in my Database is about 400 lines and i didn’t yet add anything in it.

But no matter how fat it is, actually the real problem is elsewhere :

Imagine that you call your model like this here and there in your app :



User::model()->with('telNumbers')->findByPk($userId); // has many relation

Then, you want to add a "visible" column into your table, and logically want that your app only plays with visible users.

So you have to change all pieces of your code where you retrieve an user (this is a bug source)

My option is to have something like this :

function getUser($userId, $withProfile = true, $withTelNumbers = false) {

// Two if based on params to fetch the user


Then you have only to change on part of your code to make your change.

This take much time to do but is by far more maintainable.

well, if you don’t like whatever yii default activerecord provids or it does not meet your need, you definitely could wrap up CActiveRecord creating your own version of base class on top of CActiveRecord by adding more generic functions then inherit it in all your data model.

it’s up to the nature of your requirement about data model, if it’s really something generic yii should have, you may want to suggest yii dev team in yii 2.0 feature request forum.

ok, thanks for your help.

What you’re trying to do is possible with Yii.

Use default scopes. If there is a time where you add the "visible" column, just add a default scope, and turn it into a condition=>"visible=1".

That way, all findAll()'s automatically use that scope.

Read more here: