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

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

问题描述

我正在学习 Zend Framework 和 Doctrine.

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.

在其他情况下,模型类中的方法可以方便地为表单准备数据.假设它可以有一个方法返回为 select-options 元素准备的数组,因此该方法对许多表单都很有用.

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.

如果您让模型和表单直接通信,那么在特定时间会发生什么会变得非常混乱.我会创建像 populate 方法这样的便捷方法来保持简短,但所有操作都应该从控制器启动以保持中心化并防止魔术行为".

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

只要我的 2 美分..

Just my 2 cents..

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

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