0

I am using the MVC concept (as per http://r.je/view-helpers.html) in my PHP project:

  • Controller - processes user interaction;
  • View - displays any data and or fields;
  • Model - handles all business logic, including access to data access objects.

A front controller is instantiating the MVC triads. In this structure I need the View to display html select option lists that are populated from the database, along with any user fields the various domain objects require data from. I hope this is clear.

My question is: the View cannot ask the database for the data to populate the select option list, so what is the generic application flow to "pass" the View the required form data to correctly populate the required select option list?

NOTE: The View currently gets user data (in a Request object) from the Model, and any validation errors the View has asked the Model for.

Steve Cooke
  • 447
  • 1
  • 4
  • 14
  • i don't know which framework are you using, it can be as simple as setting an object in the request object (controller), in the view just read this object and display what is needed. i usually pass a json object just in case in future i want to deploy my models/controllers/views to different servers. – Jianhong Sep 24 '13 at 01:33
  • 1
    You could have a function in your model that selects the records you need and does the processing to get them in an array or other enumerated list that you can then loop through and generate your HTML code. Your view may not be able to talk to the database directly, but you can have it talk to your model (which it looks like you are doing) which can take care of the processing up to the point of HTML creation. – Crackertastic Sep 24 '13 at 01:37
  • What is the significance of phrase "View cannot ask the database for the data"? It would seem to me that "categories" would be something that's associates with your `Library` service (maybe you use different name) in the Model layer. A view would use this service to acquire list of categories, which then it would pass either directly to one of the templates or to a [presentation object](http://martinfowler.com/eaaDev/PresentationModel.html)(which then would be used in one of templates). – tereško Sep 24 '13 at 01:47
  • Thank you all for your replies. I feel @Crackertastic has the most complete answer for my purposes. teresko provides another solution that I have not implemented yet - in a later iteration of the project I will look at what is required. Jianhong, currently the front controller simply gathers user data and passes it on to the model. The front controller DOES know about the MVC triad, but knows nothing more. My triads are not domain objects, so something needs to trigger the inclusion of a domain object in my Request object (I assume you mean set a domain object in the Request). – Steve Cooke Sep 24 '13 at 03:07
  • You might later find these two posts useful-ish: [this](http://stackoverflow.com/a/5864000/727208) and [this](http://stackoverflow.com/a/16596704/727208). – tereško Sep 24 '13 at 03:59
  • Thank you again @tereško. Both very interesting posts, and yes - that is my understanding of "Model". I've not used services in the past, but your example is clear. – Steve Cooke Sep 24 '13 at 12:01

0 Answers0