Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top