How Models Works in Joomla

Models are intended to handle a component's data processing. This usually although not exclusively involves storing and retrieving data from the database. Let's take a look at how a model could be used to retrieve records from the database so that they can be displayed in a view.

Create a model Folder in Joomla

Malls Model File in Joomla

First, we're going to create models folder into our Joomla installation in the com_noidacity folder. Once you have created models folder go inside and create malls.php file inside. So this is a model file and add the following code.

  1. <?php
  2. defined( '_JEXEC' ) or die;
  4. jimport('joomla.application.component.modellist');
  6. class NoidaCityModelMalls extends JModelList
  7. {
  8. public function getListQuery()
  9. {
  10. $query = parent::getListQuery();
  12. $query->select('place_name');
  13. $query->from('#__noidacity_malls')
  14. ->where('published = 1');
  16. return $query;
  17. }
  18. }


  1. class NoidaCityModelMalls extends JModelList
  2. {
  3. ...
  4. ...
  5. }


We have a class here defined and it's called NoidaCityModelMalls and that follows the convention of the component name model and the name of the model. It's extending the JModelList class. JModel List is specifically designed for pulling a list of records from the database, so that they can be displayed in the front-end.

  1. public function getListQuery()
  2. {
  3. $query = parent::getListQuery();
  5. $query->select('mall_name');
  6. $query->from('#__noidacity_malls')
  7. ->where('published = 1');
  9. return $query;
  10. }


Next, we define the getListQuery function. This function is one that JModelList is going to look for and it's going to call this function and get back a query object, so that it can run it against the database.

  1. $query = parent::getListQuery();


If you call the getListQuery function that just comes with JModelList by default, you get back a database query object that's fresh and ready to go. That's exactly what we want.

  1. $query->select('mall_name');
  2. $query->from('#__noidacity_malls')
  3. ->where('published = 1');


Once we have that query object, we call the select, and from, and where functions, so that we can build our database query. So we just want the mall_name from all the published malls in noidacity.

  1. return $query;


Once we've built that query object, we just return it. So this is all we need for our model to get the mall_name from all of the published malls in the database.

View Html php file of malls

In order to display those malls, we need to make a few adjustments to our malls view. So go to Views, and malls, and edit view.html.php. Add the following lines of code

  1. <?php
  2. defined( '_JEXEC' ) or die;
  4. jimport('joomla.application.component.view');
  6. class NoidaCityViewMalls extends JView
  7. {
  8. protected $header;
  9. protected $items;
  11. public function display($tpl = null){
  12. $this->header = 'Noida City Malls';
  13. $this->items = $this->get('Items');
  15. parent::display($tpl);
  16. }
  17. }


  1. protected $items; //we define the items property


  1. $this->items = $this->get('Items'); // this is where we get the items.


Notice that this is a get method for JView and it's not the actual get method that's in the model. This is because Joomla does not let JView directly touch the model. It does this abstraction to make sure that we're not trying to pull anything directly from the model that we shouldn't be doing.

So after saving the view.html.php file, we need to edit the layout file as well so that it actually displays the records.

opening mall tmpl default file

So go into tmpl, and open up default.php and add the following code:

  1. <?php defined( '_JEXEC' ) or die; ?>
  2. <h1><?php echo $this->header; ?></h1>
  3. <ul>
  4. <?php foreach($this->items as $item): ?>
  5. <li><?php echo $this->escape($item->mall_name); ?></li>
  6. <?php endforeach; ?>
  7. </ul>


and now with this in place, we can load this view in the front-end.

Data coming from database with the help of models

Go to index.php?option=com_noidacity and then view=malls. So now you'll notice in addition to the header that we had before, all the database code is in the model while all of the display code is right here in the View. That way, we don't have the two mixing with each other and if we want to adjust the view, we don't have to worry about disturbing the model and breaking something.

So models allow you to interact with the database completely separate from views. Because of the separation, you can work on HTML output in the View without worrying about accidentally breaking your database queries.