热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Smarty/views/templates中的数据库调用-DatabasecallsinSmarty/views/templates

Todayatworksomeonetriedtoconvincemethat:今天在工作中有人试图说服我:{$obj->getTableInfo()}isfine

Today at work someone tried to convince me that:

今天在工作中有人试图说服我:

{$obj->getTableInfo()}

is fine for smarty/mvc/templating because it is using an objects method. I argued that because it makes a call to the database it has no place being there and it should be in the controller (we don't actually use MVC). Am I right in my understanding of the logical separations that are used in MVC and generally in templating? Or is there something that I am missing?

适用于smarty / mvc / templating,因为它使用的是对象方法。我认为,因为它调用数据库它没有位置,它应该在控制器中(我们实际上不使用MVC)。我是否正确理解MVC中使用的逻辑分离以及模板化中的逻辑分离?或者有什么我想念的东西?

3 个解决方案

#1


You're right. He's wrong.

你是对的。他错了。

Database calls, no matter in what form, should live in the controller if you want to do MVC right.

如果你想要正确地进行MVC,数据库调用,无论采用何种形式,都应该存在于控制器中。

Obviously people piss all over what it should be and do stuff like that, but it's not the correct way.

很明显,人们会对它应该是什么感到厌恶并做这样的事情,但这不是正确的方法。

#2


Well, there are no "official" rules or anything, but I think something like that belongs in the controller. I don't do anything in my view code except display variables, nothing more complex than an if or a foreach-type loop is allowed. Certainly not calling functions that access the database. That should all be loaded by the controller, the view should only decide whether it needs to display it or not.

好吧,没有“官方”规则或任何东西,但我认为这样的东西属于控制器。除了显示变量之外,我在视图代码中没有做任何事情,没有比if或foreach类型循环更复杂的东西。当然不会调用访问数据库的函数。这应该全部由控制器加载,视图应该只决定是否需要显示它。

#3


Depends on its context and scope really.

真的取决于它的背景和范围。

Is $obj the controller or the model layer? That should answer whether or not it is valid in my opinion.

$ obj是控制器还是模型层?在我看来,这应该回答它是否有效。

In response to reading the other answers.

回应阅读其他答案。

The functions name in itself pertains it to being a simple getter method. It may make a call to the db through a controller layer. Which I would say is ok. Especially if it used some form of caching in memory. (ie the getter is the setter as you only want to cache it when it is used once.)

函数名称本身就是一个简单的getter方法。它可以通过控制器层调用db。我会说的还可以。特别是如果它在内存中使用某种形式的缓存。 (即getter是setter,因为你只想在使用一次时缓存它。)


推荐阅读
author-avatar
mobiledu2502887403
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有