Actions

J1.5

Difference between revisions of "Developing a MVC Component/Adding a Model"

From Joomla! Documentation

< J1.5:Developing a MVC Component
(cat)
m (Download: clean up and recategorisation if necessary)
Line 1: Line 1:
 +
{{Chunk:Developing a Model-View-Controller (MVC) Component for Joomla!1.5 - Contents}}
 
== Introduction ==
 
== Introduction ==
  
Line 11: Line 12:
 
The concept of model gets its name because this class is intended to represent (or 'model') some entity. In our case, our first model will represent a 'hello', or a greeting. This is in line with our design thus far, because we have one view ('hello'), which is a view of our greeting.
 
The concept of model gets its name because this class is intended to represent (or 'model') some entity. In our case, our first model will represent a 'hello', or a greeting. This is in line with our design thus far, because we have one view ('hello'), which is a view of our greeting.
  
The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case 'hello', followed by 'model', followed by the model name. Therefore, our model class is called HelloModelHello.
+
The naming convention for models in the Joomla! framework is that the class name starts with the name of the component (in our case 'hello'), followed by 'model', followed by the model name. Therefore, our model class is called HelloModelHello.
  
 
At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string 'Hello, World!'.
 
At this point, we will only model one behaviour of our hello, and that is retrieving the greeting. We will thus have one method, called getGreeting(). It will simply return the string 'Hello, World!'.
  
Here is the code for our model class:
+
The code for the model at site/models/hello.php:
 
   
 
   
 
<source lang="php"><?php
 
<source lang="php"><?php
Line 57: Line 58:
 
== Using the Model ==
 
== Using the Model ==
  
The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called 'Hello', our 'Hello' model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method. (If the model had not followed this convention, we could have passed the model name to [http://api.joomla.org/Joomla-Framework/Application/JView.html#getModel JView::getModel()])
+
The Joomla! framework is setup in such a way that the controller will automatically load the model that has the same name as the view and will push it into the view. Since our view is called 'Hello', our 'Hello' model will automatically be loaded and pushed into the view. Therefore, we can easily retrieve a reference to our model using the JView::getModel() method. (If the model had not followed this convention, we could have passed the model name, as a parameter, to [http://api.joomla.org/Joomla-Framework/Application/JView.html#getModel JView::getModel()])
  
 
Our previous view code contained the lines:
 
Our previous view code contained the lines:
Line 65: Line 66:
 
To take advantage of our model, we change this line to:
 
To take advantage of our model, we change this line to:
 
   
 
   
<source lang="php">$model =& $this->getModel();
+
<source lang="php">$model = &$this->getModel();
 
$greeting = $model->getGreeting();</source>
 
$greeting = $model->getGreeting();</source>
  
Line 94: Line 95:
 
     function display($tpl = null)
 
     function display($tpl = null)
 
     {
 
     {
         $model =& $this->getModel();
+
         $model = &$this->getModel();
 
         $greeting = $model->getGreeting();
 
         $greeting = $model->getGreeting();
 
         $this->assignRef( 'greeting', $greeting );
 
         $this->assignRef( 'greeting', $greeting );
Line 113: Line 114:
 
<install type="component" version="1.5.0">
 
<install type="component" version="1.5.0">
 
  <name>Hello</name>
 
  <name>Hello</name>
  <!-- The following elements are optional and free of formatting conttraints -->
+
  <!-- The following elements are optional and free of formatting constraints -->
 
  <creationDate>2007-02-22</creationDate>
 
  <creationDate>2007-02-22</creationDate>
 
  <author>John Doe</author>
 
  <author>John Doe</author>
Line 158: Line 159:
  
 
We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.
 
We now have a simple MVC component. Each element is very simple at this point, but provides a great deal of flexibility and power.
 
== Other Articles in this Series ==
 
[[Developing a Model-View-Controller Component - Part 1]]
 
 
[[Developing a Model-View-Controller Component - Part 3 - Using the Database]]
 
 
[[Developing a Model-View-Controller Component - Part 4 - Creating an Administrator Interface]]
 
  
 
== Contributors ==
 
== Contributors ==
Line 173: Line 167:
 
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8109/29434/com_hello2_01.zip com_hello2_01]
 
The component can be downloaded at: [http://joomlacode.org/gf/download/frsrelease/8109/29434/com_hello2_01.zip com_hello2_01]
  
[[Category:Development]]
+
[[Category:Component Development]]

Revision as of 07:23, 18 September 2012