views:

251

answers:

3

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?

+1  A: 

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.

Chad Birch
+5  A: 

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.

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

Paolo Bergantino
A: 

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.

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

jim
That was his argument... 'It is cached after the first call...' But... it would be 'cached' if you put it in a variable and passed it to the view as well
SeanJA
On demand caching will only work if it is only cached-on-demand. ie on the first call. Any other way and it wouldn't work as intended. Something in that function has to make a call somewhere. The view layer is not making that call to the db. It is making a call to the object. The object is making a call to x.
jim
Further, if the actual db call is in that function it means that something is wrong. There needs to be the model layer in-between. If it is making a call to the controller layer then that is fine imo.
jim
I believe it went something like:$obj->tableInfo() ...class tableInfo(){ return getTableInfo() } /class...... getTableInfo(){ query_database }
SeanJA
Sounds like something from my work, mike is that you? I will say that that looks like a data access layer (controller), so yea, that shouldn't be like that in mvc. Of course you aren't using a defined mvc method so it doesn't really matter (other than the psychiatrist bills you will be paying later on in life from going insane) :P
jim