Yii Framework Forum: Where To Store Model Methods - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Where To Store Model Methods Rate Topic: -----

#1 User is offline   Leto 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 11-February 13

Posted 12 February 2013 - 09:27 AM

Hey,

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.
0

#2 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 12 February 2013 - 09:56 AM

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.
I enjoy the Yii.sy coding life here.
0

#3 User is offline   Leto 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 11-February 13

Posted 12 February 2013 - 10:16 AM

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()->findByPk($userId);
User::model()->with('profile')->findByPk($userId);
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.
0

#4 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 12 February 2013 - 10:28 AM

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.
I enjoy the Yii.sy coding life here.
0

#5 User is offline   Leto 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 11-February 13

Posted 12 February 2013 - 10:33 AM

ok, thanks for your help.
0

#6 User is offline   yJeroen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 06-September 11
  • Location:The Netherlands

Posted 12 February 2013 - 04:56 PM

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:
http://www.yiiframew...ar#named-scopes
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users