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 Answers 3

    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.
    4
    • I asked because it seemed like a semantic problem, but I am using Doctrine 2 and from that standpoint I do not see how having database queries in a class that I would actually instantiate and send to the database makes sense, unless DBAL entities do not represent entities, in which case I don't think I have time to change the entire semantic basis of my project. With regard to the article I linked, do you disagree with what it is saying or do I just not understand MVC enough to see that your's and theirs' ideas run in parallel?CommentedMay 24, 2020 at 21:23
    • @ScottAnderson The article seems in line with what I said. I edited to add some quotes in relation with my statements. I have no expertise in PHP and Doctrine. But from what I understand, Doctrine is an ORM. It should play a key role in the Model implementation. I'm not sure it is intended to replace the model (e.g. there is no business logic. If the change in price would require to update other data, your ORM would not know, but the model should ensure such consistency).CommentedMay 24, 2020 at 22:05
    • I have read and re-read and I think I am getting the picture a lot better. If, by my misguided approach it did happen that: my Model was just a dumb ORM object, my Controller was a glorified gateway to the database, and my View was really just taking Model instances and crunching the data into HTML to inject, would that be tending towards a different pattern?CommentedMay 25, 2020 at 16:13
    • @ScottAnderson I think this explains rather well what happened and the initial confusion. It doesn’t lead to a fundamentally different pattern: database access (via ORM or not) is always in the model. Note however that the model does not need to be behind a monolithic facadeCommentedMay 25, 2020 at 19:27
    -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."

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

      6
      • Thankyou for the response. So you are saying that it wouldn't break MVC ideology to have a getItem($id) or getAllItems() method in my controller which the view uses?CommentedMay 24, 2020 at 20:59
      • 1
        I updated my answer, please refer to it.CommentedMay 24, 2020 at 21:05
      • Thankyou, I believe I understand correctly - you have saved me some unncessecary rigging of the Doctrine EntityManager on a view :)CommentedMay 24, 2020 at 21:08
      • 1
        Nothing in MVC dictates use of ORM.CommentedMay 25, 2020 at 6:07
      • 2
        @OmidKamangar "The controller should use an ORM" remains and makes ORM appear to be a requirement of MVC. "preferably using an ORM" - please make clear that this is your preference not MVCs. MVC can be used even when the database doesn't exist. Your answer will do much better when it doesn't invite confusion.CommentedMay 25, 2020 at 7:10

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.