Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Design Help - Multiple Models Widget
  • Hey Peeps, I've been playing around with a few PHP Frameworks, and got going with fuelPHP quite quickly so I'm going to try and stick with it, looks great so far! Ok, so now down to the problem, I'm a little brain dead at the moment so some help on my design using fuelPHP would be much appreciated.
    I've put together a dynamic multiple choice question/answer system that revolves around sets of questions, with a "question set" being made up of 3 tables. I have setup a basic fuelPHP app and done the scaffolding so its all connected and I can view,add,edit,delete each individually. For the main controller, I have a function in there that gets results from each of the 3 tables/models, passing those to the view, where I display them group together in each question set, thats a hack for sure, because to edit the "question set" you have to go to each of the edit views for each of the elements for each of the models that make up a "question set" Now each of these question sets is a repetitive thing right, made up of the same fields from the same models each time, so it would make sense to make a new model that is made up of the 3 original models, and create a widget type of thing that can be used in the other views to display.
    That would need a controller as well right? Where I can do the logic of taking the data out of the "question set" model, separate it and save each of the individual models. For example; User edits the "question set", say the title text (which is from one table/model) and adds another question option(again which is from another table/model) and clicks save, the controller would take the POST info containing the new "question set" model separate the data into each of the 3 models and call the save functions of each of those models individually to update the DB. So what would be the best way to go about solving this and essentially building a model,view(widget template) and controller that is made up of multiple models ?
  • I would create a view that produces a single question set (your widget), and a viewmodel that does the processing to produce the question data (to abstract the logic from your controller, and to avoid having to create special models). From the controller, load the template view for your page, then loop through your questions, calling the viewmodel for each of them, and assigning the result (a Viewmodel object) to the template view.
  • Great thanks for the response WanWizard!
    So a ViewModel is the key, I'll start my solution with that.
  • Basically, everywhere were you can use a View object, you can also use a Viewmodel object. The Viewmodel allows you to add extra logic to your views, without adding clutter to your controllers (it's view logic, you don't want that replicated in your controller) or your views (which should be simple and about layout only). I use them a lot, even for simple things like fetching an array from a model to produce a dropdown in the view.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion