About Fat Model

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.

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! :]

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.

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.

I tend to use CFormModel for a business logic even when I already have a CActiveRecord model for it.

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.

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.

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.

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 ),

    ...

  );

}



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 :)

Do anyone know about something like ‘partial class’ of C#, if Yii framework has similar conception about that?