3

I am at the stage of implementing a my 1st ever view, after developing a Model and Controller, however there is a problem.

I have been reading this article on MVC, which is what I have been aiming for when implementing, but I kind of went off-script when making the Controller because I didn't remember to look back at it, not that I think it would have been all that much help.

Currently my controller contains the methods:

    public function add($name, $price, $dimensions): string

    public function remove($sku): void

    public function removeAll(): array

    public function updatePrice($sku, $price): void

But when coming to writing the View I realise that I would have to implement database access in order to list entities, unless I ask the controller for the data from the model. That seems kind of wrong to me, but given some of the description in the article mentioned above it seems like this might required.

It seems most neat to me to supply 'get' functionality from the class which already has database access. In short, should a View interface a database directly?

3 Answers3

5

In a true MVC, the controller takes the input from the user, and translates it into commands either for controlling the views (e.g. create a new view or close a view) or for the model (e.g. updating the price, saving changes,...).

The methods that you describe do not belong to the controller but to the model: adding priced items, updating prices, removing stock keeping units (sku) and remove all the items is the responsibilitiy of the model. To quote your article:

the Model is the name given to the permanent storage of the data used in the overall design. It must allow access for the data to be viewed, or collected and written to, (...)
The Controller can be summed up simply as a collector of information, which then passes it on to the Model to be organized for storage, and does not contain any logic other than that needed to collect the input. (...)
The most common mistake made by developers is confusing the Controller for a gateway, (...)

So the controller shall not just be a gateway to the database. All database interactions belong to the model: the controller and the view know nothing about the database: they only know about the model.

Additional remarks :

There are several MVC variants that all share the principles above. In your article, there is one controller per view.

An ORM makes the mapping between objects (application) and tables (database) and facilitates access to the database. In an MVC architecture, the ORM is used to implement the model. It should not replace the model.

Different architectures may distribute responsibilities differently. But it's no longer MVC:

  • If a controller acts as middleman between views and model, and views querry the controllen, then it's probably no longer a controller but a presenter in an MVP
  • If a view has furthermore some databinding that is synched with the model via the controler, it's probably no longer a controller but a view-model in an MVVM
  • If a controller has some business logic and database responsibilities that are not in the model, you probably made something wrong in the MVC, or there is a misunderstanding about the architecture that implements and EBC pattern in which the controller has a different purpose.
Christophe
  • 81,699
-1

A View should be as dumb as possible. It should not access the database. Fetching the data is the responsibility of the Controller.

The controller should fetch the data from the database (preferably using an ORM) and feed it to the appropriate view.

Edit

Let's assume you are writing a web app to manage books. You write a BooksController and you have an action to show the details of a book. Let's assume you call that action show. In the show action, you query the database for the book you want to show the details of (preferably using an ORM). You store it in a variable and pass in that variable to the view to be rendered. How you pass that variable depends on how you have implemented the rendering logic.

Edit 2

As @candied_orange mentioned in the comments, using an ORM is not a requirement to use MVC. But IMHO it is worth using an ORM, because it saves you a lot of headache (albeit introducing some new headaches).

-1

Let me simply comment here that: "MVC is a fairly-abstract guideline." Not every situation conforms to it – some old-hands, maybe including me, would say that "none of them do." But (well, to me at least), that's actually okay. "The general idea is useful, nonetheless," as a general strategy for the real meat-and-potatoes of it: "separation of concerns."