Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zend-framework doctrine, and mvc pattern: what kind of layer should connect data between models and forms?

I am learning Zend Framework and Doctrine.

I am wondering what is the best practice to connect forms to models and vice versa.

In some cases it is handy to load data from model in form class. Lets say a very unique class which use many models.

In other cases it is convenient to have methods in model class which prepares data for forms. Lets say it could have a method which returns an array prepared for select-options element, so this method will be useful for many forms.

I would like to have consistency and always keep this logic in one layer.

I think controller is not the right place because I want to keep it clear and simple.

What is your practice to achieve this goal (connect models to forms) ?

- I am coming into conclusion that I should prepare my models for all my needs. If I have to deal with many models I will have a service layer (is it the right term?) which will connect those models. So the model or the service will have methods to hydrate data for forms. And it will be able to accept data from form values.

like image 366
Ski Avatar asked Nov 05 '22 12:11

Ski


1 Answers

I think the controller is the best place for connecting models and forms. If you want to prevent a lot of code for populating the form create a populate method on the form that accepts a model.

If you let the models and forms communicate directly it will become very confusing what will happen at a particular time. I would create convenience methods like the populate method to keep things short, but all actions should be initiated from the controller to keep things central and prevent "magic behaviour".

Just my 2 cents..

like image 121
Jeroen Avatar answered Nov 29 '22 20:11

Jeroen