0

This isn't a question about getting model data into another view. I can create a huge form with data from multiple models. But, is that necessary? Can a model display its view within the context of another view (similar to an object displaying itself)? If so, how?

1

Not by default, no. The model and the view basically don't talk to each other at all, all interaction between them is handled by the controllers. The reason the model is attached to the view is because it gets set as a property in the JControllerLegacy::display() method.

The model stores its associated view name as a string in its 'name' property, but it's not much use on its own as models don't have a display() method. Here's how a display task works:

  • When it's time to display a page _GET has 'task' set to 'display' and 'view' set to whatever view you're going to.
  • Your component's root .php file gets your root controller.php (which extends JControllerLegacy) and executes the method corresponding to __GET's 'task' value, yourController::display().
  • yourController::display() gets the view class using __GET's 'view' property.
  • yourController::display() uses the view class' name to get the associated model class and sets it as a view class property.
  • yourController::display() executes yourView::display().
  • yourView::display() does its thing and outputs its template.

You could override JControllerLegacy::display() in yourController::display() then also set the view as a property of the model, but you're better off chaining views' display methods instead.

  • So for example, if I have a student model and a class model (along with associated views), and I want to display the student info and the classes s/he is taking in a combined view then I'll need to duplicate those forms in a new view and populate it with data from each model? There isn't a better way to display combined views? And, this might be expanding the question a little but how does save get handled with multiple models? – nimble Aug 9 '16 at 14:38
  • The two best ways, depending on exactly what you want to do are: - Create a single model that gets data from both forms and displays them in a single view, possibly use layouts if you're using the same layouts in multiple views. - Create a single model to get the data from both forms and use the controller to attach that model to both views you want to use. Save can be defined in a custom controller class so it doesn't use the model at all, or you can use the model from above to handle saving data from multiple forms, much like it would retrieve it. – chrBrd Aug 10 '16 at 1:09
  • Do you know of a component I could look at as an example? For saving I'd prefer to let each model save its own data. – nimble Aug 10 '16 at 14:04
  • Afraid not, no. If you want each model to save it's own data, in your controller you can make the save method invoke each model's individual save methods. There's absolutely no need to do that if you create a new model with its own save method though, you can very easily just set it to save each data type exactly as they're saved in the original models. If you want to use the original save methods you're going against the whole point of your question; you'll still need to get each form's model in the controller beforehand to invoke their seperate save methods. – chrBrd Aug 10 '16 at 17:56
  • I'm not following what you mean when you say that "you can very easily just set it to save each data type exactly as they're saved in the original models". I assume that you mean that the save() method for each of the models should be called. – nimble Aug 10 '16 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.