S
S
Sergey Nitka2014-02-04 12:44:33
Zend Framework
Sergey Nitka, 2014-02-04 12:44:33

zend framework. MVC. How to code 'fat' models correctly?

Today in one old deferred project I came across the fact that I have from 5 to 10 thousand lines of code in the main models of the project. And it became very painful to change, maintain, and add new functionality.
I have a feeling that the best practitioner is talking nonsense about fat models.
I explain in more detail.
Models have some methods that are cumbersome, use and manipulate data, but are used only once in a project. For example, the user registration method. 400 lines of code. Used only in RegisterController. For example, methods for displaying requests and announcements, depending on the type of authorized user and access to data. 500 lines of code. Used strictly once per AdvertisementController. And so on
. There is an entity Model_Advertisement.
In which visually I saw the following picture. Heavy methods used once are separated visually into Create / Status Change / Output.
Is it bad if I do this?
- Model_Advertisement_Core - methods that are used many times
- Model_Advertisement_Fetcher - methods that are used once for data output
- Model_Advertisement_Changer - methods that -//- for changing data
- Model_Advertisement_Creator - methods that -//- for creating requests
Yes.... It turns out that I break heavy CRUD into logical components in the model
Who can say or advise on this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vit, 2014-02-04
@fornit1917

I do not use Zend, but Yii, but the essence is the same. And between the model and the controller I make another layer of abstraction - a layer of services. These are just classes whose methods perform some logical transactions by manipulating models and other services. Easy to reuse, easy to test, no problem with huge files.

S
Sergey Nitka, 2014-02-04
@Rumzik

That is, you can simply create a class

class Service_Advertisement{
   public static function action1(){ /**Здесь манипулируем моделями и параметрами запроса**/   }
   public static function action2(){  /**--//--**/  }
}

and in the controller
class AdvertisementController extend Zend_Controller_Action
{
  public function createAction(){
       //render in view or render in json format (auto detected context)
       return Service_Advertisement::action1($this->_getParams());
  }
}

By the way, in Yii I see the concept of static methods in models. It's actually convenient. But is it right or not????
It’s bad in Zend, because the kernel wrapper is not static and the pdo adapter is initialized in the constructor when the object is created
and you have to write every time
$model = new Model_Advertisement();
$advertisementObj = $model->findRow($id);
$model = new Model_Users();
$userObj = $model->findRow($user_id);
$model = new Model_Stat();
$model->calculateStatisticInformation($advertisementObj,$userObj);

On Statics, it would look easier, it seems to me.

C
cawakharkov, 2014-02-07
@cawakharkov

there should not be any "fat" models or controllers, by the way, I often met this with uyy developers (no offense will be said).
Use services and SOA architecture vk.com/wall-175_45236

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question