I'm creating an MVC Component for Joomla 2.5 as a front end to another PHP database system I've built. Instead of just wrapping the other system's web interface in the wrapper component I'm building a native Joomla component that talks calls methods in other system's classes.
As such I have implemented a HTML form in one of my views which is to accept some input obviously but also a file upload. From here the component should send this input to my class's method for processing it, then the response will be fed to a second Joomla component view.
It seems to me that the the handling of the input should be done in a Model and probably receiving the processed output should also be done by the model. The Controller would then feed the model data to the second view?
I've gone through the Developing a MVC Component tutorial but it doesn't really give me much of an idea of how this should work. Can anyone give me a rough psuedocode idea of how this should fit together or point me to an appropriate tutorial or example of another component that does a similar thing?
Short answer, Yes your model should handle all retrieving and saving of an items details i.e. its row in the database or physical files.
So, for a longer answer, take the way
com_content
works as an example.In the front-end you create a new article (e.g. via "Submit an Article" item of the "User Menu"). This is sent of as a
GET
request, with values similar to this:This request travels as follow:
index.php
receives the request andcom_content/content.php
is called when the components is required (i.e. com_content entry point).content.php
creates controller via theJController
class.JController
looks at the inputs (i.e. params sent in the originalGET
request), figures out what component it's in and tries to firstly load a file calledcontroller.php
in the extensions folder and then looks for a suitable class in that file. (In this caseContentController
)content.php
then tells the$controller
object to call theexecute()
with the relevant task, using this$controller->execute(JRequest::getCmd('task'));
As you can tell from the
GET
request, notask
has been set in this case so the__default
task is used ($doTask = $this->taskMap['__default'];
) In a JController the default task defaults todisplay
, unless you override it.This causes the
display()
method in theContentController
class (com_content/controller.php
) to be called.After some basic checks
display()
then calls theparent
version of itself i.e.parent::display($cachable, $safeurlparams);
The
JContoller
version ofdisplay()
does all the basic work like getting the view name (form
) and layout (edit
) and using those to load the right view object (ContentViewForm
).Then it loads the model & adds it to the view as the default model. (In this case the model is
contentModelForm
) It loads the right model based on the view name (form
) and themodel_prefix
for the component. Themodel_prefix
is setup byJController
during it's__construct()
method, by taking the name of the component 'Content' & appending 'Model' to it.After a bit more setup, the
display()
method of the viewContentViewForm
is called, which is where the model data is loaded (if we were editing an article the same calls to the model would load the existing article based on a extra parameter in theGET
containing the article ida_id=99
).It also loads the Article
form
(com_content/models/forms/article.xml
) at this point for use in thetmpl
fileedit.php
.So, to setup the input side the
content
fields are coming from the model (albeit an empty model for a new article) and the attributes of the fields are defined in the models matching form.The saving of changes to the Article form takes a very similar path.
GET
portion of the request contains your article id (a_id=99
) and the option parameter that point Joomla at your component (option="com_content"
)POST
portion contains the form (jform
) as an array, the task being performed (task=article.save
) and a few other housekeeping parameters.As a result the controller type instantiated by
JController
this time is aContentControllerArticle
which extendsJControllerForm
(which is used for handling form submissions etc).Remember that dot notation task values are of the form [sub]controller.method.
The
save()
method of theContentControllerArticle
object is called briefly before calling it's parentsave()
method inJControllerForm
.At this point the
save()
method does things like check access permissions, validate data against any validations defined in the form and then load the model and passes the form data to the modelssave()
method.Does that help?