zend-framework原理和mvc模式:什么样的层应该在模型和表单之间连接数据? [英] zend-framework doctrine, and mvc pattern: what kind of layer should connect data between models and forms?

查看:76
本文介绍了zend-framework原理和mvc模式:什么样的层应该在模型和表单之间连接数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Zend框架和学说。



我想知道将表单连接到模型的最佳做法是什么,反之亦然。



在某些情况下,可以方便的从form中加载模型中的数据。让我们说一个使用很多模型的非常独特的类。



在其他情况下,在模型类中使用准备表单数据的方法很方便。让我们说它可以有一个返回一个为select-options元素准备的数组的方法,所以这个方法对于许多形式都是有用的。



我想要保持一致性,



我认为控制器不是正确的地方,因为我想保持清晰简单。



您的实践是实现这一目标(将模型连接到表单)?



-
我得出结论,我应该准备我的模型满足我的所有需求。如果我必须处理很多型号,我将会有一个服务层(是否是正确的术语?),这将连接这些模型。所以模型或服务将有方法来水合数据的形式。它将能够接受表单值的数据。

解决方案

我认为控制器是连接模型和表单的最佳位置。如果你想防止大量的代码填充表单,在接受模型的表单上创建一个填充方法。



如果让模型和表单直接沟通会变得非常混乱,在特定时间会发生什么。我将创建方便的方法,如填充方法,以保持简短,但是所有的操作都应该从控制器启动,以保持中心,防止魔法行为。



我的2美分..


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.

解决方案

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

这篇关于zend-framework原理和mvc模式:什么样的层应该在模型和表单之间连接数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆