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

房东的猫 提交于 2020-01-01 10:48:08

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/2940442/zend-framework-doctrine-and-mvc-pattern-what-kind-of-layer-should-connect-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!