在 Zend Framework 中控制访问/处理传递给 View 的对象/模型数据的最佳方法
我想将数据传递给视图,我脑子里有两个选择(如果您知道更好的方法,请提及)。
我正在使用 Zend_Based ORM 系统,并以一种方式编码,如果我在数据库中添加新字段,它会在模型中自动可用。
第一:我将模型的数据转换为数组并将数组传递给视图。这样我将在视图中获得所有可用数据,但模型的功能/操作将不可用。如果我需要特定的功能,我将编写视图助手,而模型中可能已经编码了相同的功能。例如获取特定格式的日期。
第二:或者我将完整的模型对象传递给视图,这样我将拥有可用的模型的所有功能,但视图将能够访问模型的保存功能,这是一件坏事。我可以在模型中添加更多功能以使其只读,但这将是额外的工作。
任何建议哪种方法更好。
I want to pass data to views, and I've two options in my mind (if you know a better approach, please mention).
I am using Zend_Based ORM system, and coded in a way that if I add a new field in database, its automatically available within the model.
1st: I convert the model's data into array and pass the array to the view. This way I will have all the data available within the view, but model's function/operations will not be available. And incase I need specific functionality, i will be coding view helpers while there are chances that the same functionality is already coded within model. e.g. a getting a date in specific format.
2nd: Or I pass the complete model object to the view, this way I will have all the model's functions available, but view will be able to access model's save function which is a bad thing. I can add some more functionality within model to make it read-only, but it will be extra work.
any suggestions which approach is better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 MVC 原则,让视图允许访问模型是完全可以的。因此,将完整的模型传递给视图。
顺便说一句,传递数组将复制您的数据(按值),而传递对象则不会(按引用)。 (假设 PHP5)。大型数组可能会影响您的性能。
According to the MVC principle it's perfectly fine to let the View allow access to the Model. So, pass the complete Model to the View.
By the way, passing arrays around will copy your data (by value), while passing objects around will not (by reference). (Assuming PHP5). Large arrays might affect your performance.