Yii Framework Forum: About Fat Model - Yii Framework Forum

Jump to content

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

About Fat Model Rate Topic: -----

#1 User is offline   James Hong 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 23-September 13

Posted 24 September 2013 - 04:05 AM

Hello everyone, I'm new to Yii framework, before I check this framework, I always think the right pattern should keep Model simple, and controller or another biz layer having all logic. I still couldn't understand FAT model will be more reusable? special when the model is huge. Does anyone can sharing what do you think about this?

I'm not also thinking about FAT controller, but something like TIN controller, model, and having another FAT biz logic layer, for model is auto generated by Gii, so what I think will the better don't touch it, just extends it, I know C# having a way calling 'partial class', it just keep the auto-generated part, and you still can create a new class with the same class name to code your biz logic, so the model can be updated anytime you want, and your biz logic just remain there. When you have a total look, it's FAT model, but just separated into pieces(different files but same class).

Maybe my issue is just want a way to 'update my model anytime I want, and keep my customize biz logic there', any solution or advice will be appreciated.

Thanks in advance.
0

#2 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,193
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 24 September 2013 - 04:23 AM

Well... I can't tell you much about business logic from my point of view, as I don't have enough experience in this area. But, even so, let me drop my personal opinion.

You can look at it from the very basic point of view (at least I'm doing so). If you move most of business logic into model, you can use it in any controller (or even view), that is using particular model. While, when you encapsulate logic in the controller, you won't be able (to easily) to access it from another controller.

I don't know, how much does it follows (or violates) MVC model and professional business-logic-oriented programming, but I always found it really useful to have all data-related logic, methods and functions inside model not, in the controller.

So, I'm -- opposite to you -- supporting fat models and thin controller.

Plus, good question, if moving code / business logic from model to controller does make application more efficient, less resource-hunger and running with better performance, or is it only code organisation and following (or not) some standards? I don't know. If having fat controllers and thin models does make application better, faster, more secure, than I have to revise my point of view. If not, if this is purely design pattern, then I'm strongly supporting the idea, that it is the coder and only the coder (no some project manager, IT director or coding standard designer) to tell, what and where should be placed. You're coding, you're responsible for the effects, so make your coding (and life) as comfortable as possible! :]
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#3 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 773
  • Joined: 02-July 10
  • Location:Central Poland

Posted 24 September 2013 - 04:30 AM

also - take a look at giix generators (extension to standard gii), which implement slightly different model classes - BaseModel with generated stuff which can be overridden by subsequent calls to gii generator and final Model class which is generated only at first run and left intact later. This way you can simply put your logic in this final class and still regenerate base model.
red
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 24 September 2013 - 05:29 AM

I think Yii's CActiveRecord model is best suited for a simple and light-weight data model. It's very convenient and effective for a smaller data model.
But for a larger and heavier data model, it tends to become too fat a chunk of code, because it does not differentiate the layers of the data model... especially the model entity layer and the business logic layer.
In order to make things simple and controllable, I believe we should divide a CActiveRecord model into several layers of model class when we are dealing with a fat model.
Giix would be a good tool for it.
Also I like the concept of "filter model" by Mike.
https://github.com/m...xcrudcontroller
I tend to use CFormModel for a business logic even when I already have a CActiveRecord model for it.
0

#5 User is offline   James Hong 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 23-September 13

Posted 24 September 2013 - 05:43 AM

So many thanks for your quick responses. I'm agreed with 'FAT model', just before I heart 'FAT model', I always keep my model 'clean', and I still think it's right, because of maybe you need to regenerate or update your model many times, so if you put all your customize biz logic in a model, you need to merge it again after you update your model. The 'FAT model' conception is good, but just maybe make anyone feeling confusing like me(Thinking about Keep Model Clean).

Does anyone know about 'FAT model' just means you'd better to put all your logic inside a model? if that I'll disagree the 'FAT model' is better. It is easy to misunderstand the conception I think.

hello redguy, thanks for your recommend, maybe that's what I want now, I will try it. It will keeps my model clean I think, thanks.
0

#6 User is offline   James Hong 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 23-September 13

Posted 24 September 2013 - 09:21 PM

Hello, I just now found a new way to keep the 'FAT model', and changed a bit for it's parents class, not extends from CActiveRecord directory but using my customize base model.

Yesterday, I was struggling for a way can use my default value setting of database, but it seems a little tricky, Yii framework set the default value of timestamp or datatime type properties to "" but no null, so database couldn't recognize it, so the result is '0000-00-00 00:00:00', anyone have some good solutions for this?

I'm still searching a way to separate the FAT model to brothers model, Keep my original model clean. any ideas or opinions are appreciated.

Thanks in advance.
0

#7 User is offline   explorer 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 69
  • Joined: 26-July 13

Posted 25 September 2013 - 03:54 AM

View Postsoftark, on 24 September 2013 - 05:29 AM, said:

I think Yii's CActiveRecord model is best suited for a simple and light-weight data model. It's very convenient and effective for a smaller data model.
But for a larger and heavier data model, it tends to become too fat a chunk of code, because it does not differentiate the layers of the data model... especially the model entity layer and the business logic layer.
In order to make things simple and controllable, I believe we should divide a CActiveRecord model into several layers of model class when we are dealing with a fat model.

Yes, I feel the same about this, sometimes I put too many things in the model.

May I ask these:
- is it a good practice if I just extend the model class to separate model entity and business logic layer?
- how about in Yii 2, is there any separation for the model (another framework made a presenter class for this issue)?

Thanks.
0

#8 User is offline   redguy 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 773
  • Joined: 02-July 10
  • Location:Central Poland

Posted 26 September 2013 - 02:42 AM

View PostJames Hong, on 24 September 2013 - 09:21 PM, said:

Hello, I just now found a new way to keep the 'FAT model', and changed a bit for it's parents class, not extends from CActiveRecord directory but using my customize base model.

Yesterday, I was struggling for a way can use my default value setting of database, but it seems a little tricky, Yii framework set the default value of timestamp or datatime type properties to "" but no null, so database couldn't recognize it, so the result is '0000-00-00 00:00:00', anyone have some good solutions for this?

I'm still searching a way to separate the FAT model to brothers model, Keep my original model clean. any ideas or opinions are appreciated.

Thanks in advance.

it is not cased by yii, but by the fact that you submit form with empty value (empty string, not "null" value) for date field. Yii assigns this empty value to attribute and it is your responsibility to handle it properly.
Simplest solution is to add default value mapper in rules:
rules() {
  return array(
    array( 'date_field', 'default', 'setOnEmpty'=>true, 'value'=>null ),
    ...
  );
}

red
0

#9 User is offline   James Hong 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 23-September 13

Posted 30 September 2013 - 08:26 PM

Thanks for your reply, and I also noticed about that already. What I mean is why Yii just default those values to null but not empty. As I know null will not take memory but empty will, event you didn't use it. I think default to null is more common but not empty. Any way, may I need to change the Gii template to walk around this problem so I don't need to do it manually. I'm a lazy man :)


View Postredguy, on 26 September 2013 - 02:42 AM, said:

it is not cased by yii, but by the fact that you submit form with empty value (empty string, not "null" value) for date field. Yii assigns this empty value to attribute and it is your responsibility to handle it properly.
Simplest solution is to add default value mapper in rules:
rules() {
  return array(
    array( 'date_field', 'default', 'setOnEmpty'=>true, 'value'=>null ),
    ...
  );
}


0

#10 User is offline   James Hong 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 23-September 13

Posted 30 September 2013 - 08:30 PM

Do anyone know about something like 'partial class' of C#, if Yii framework has similar conception about that?
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