Fetching the Single Record from Joomla

In most components, you'll have at least one view where you display the contents of a single record. Let's take a look at a standard way of doing this in Joomla.

Create tables folder in the backend

First, let's create tables folder. This tables folder needs to go into the backend of the component. Even though it's going into the backend of the component, the frontend of the component is going to know to look in the backend for these classes. This is just the standard that Joomla has set for these table classes.

So let's go to the backend of the component, in administrator>components >com_noidacity and we are going to create place.php file inside tables folder and add the following code

  1. <?php
  2. defined( '_JEXEC' ) or die;
  3.  
  4. class NoidaCityTablePlace extends JTable
  5. {
  6. public function __construct(&$db){
  7. parent::__construct('#__noidacity_places','place_id',$db);
  8. }
  9. }
  1. class NoidaCityTablePlace extends JTable
  2. {
  3. ...
  4. ...
  5. ...
  6. }

So inside of place.php is a class called NoidaCityTablePlace and that follows the naming convention of the name of the component, table and then the name of the table that we want to work with.

  1. public function __construct(&$db){
  2. parent::__construct('#__noidacity_places','place_id',$db);
  3. }

And so here, we are just overriding the constructor which by default takes in a database object(&$db) and we are right away calling the parent constructor and we are passing in the name of the table(#__noidacity_places) we want to work with along with the key that we want to use which is place_id, and then we are passing in again the database object($db).

What this class allows us to do is it allows us to work with all of the create, read, update, delete functionality that we would ever want to perform on the noidacity_places table in the database. That way we don't have to write, select and update and insert statements, we can just do it all through here.

So let's go back to the frontend to actually use this table class.

Create a place file inside the model

Go to components>com_noidacity and go to the models folder because we are going to add a new model. Go to the models folder and create place.php and add the following code.

  1. <?php
  2. defined( '_JEXEC' ) or die;
  3.  
  4. jimport('joomla.application.component.model');
  5.  
  6. class NoidaCityModelPlace extends JModel
  7. {
  8. public function getItem()
  9. {
  10. $place_id = JRequest::getInt('id');
  11.  
  12. $row = JTable::getInstance('place','NoidaCityTable');
  13. $row->load($place_id);
  14.  
  15. return $row;
  16. }
  17. }

We have declared another model class here that we are going to use. This model class has a function called getItem and getItem is pulling in the id from the getURL request.

  1. $place_id = JRequest::getInt('id');

So here we have getInt the id from the URL and storing it in place_id and notice that we are using JRequest to get this value and we are using the getInt function and that's making sure that we're getting an integer and not a string or any other value that is not an integer

  1. $row = JTable::getInstance('place','NoidaCityTable');

Then we are getting a row object. So we are getting a row object here through JTable::getInstance by passing in 'place' and 'NoidaCityTable' and Joomla assembles all this into NoidaCityTablePlace and returns the row object.

  1. $row->load($place_id);
  2. return $row;

Then once we have the row object, we can call the load function passing in the place_id and that will load the id that matches in the database and finally we return the row object with the data preloaded.

So we are finally going to add a new view.

place file in the views

Create the view.html.php file inside the view of our com_noidacity component, and add the following lines of code into view.html.php file

  1. <?php
  2.  
  3. defined ( '_JEXEC' ) or die;
  4.  
  5. jimport('joomla.application.component.view');
  6.  
  7. class NoidaCityViewPlace extends JView
  8. {
  9. protected $item;
  10.  
  11. public function display($tpl = null)
  12. {
  13. $this->item = $this->get('Item');
  14.  
  15. parent::display($tpl);
  16. }
  17. }
  18.  
  1. public function display($tpl = null)
  2. {
  3. $this->item = $this->get('Item');
  4.  
  5. parent::display($tpl);
  6. }

You will notice we have a display function and it is getting the item through the getItem function in the model, but it's calling it indirectly, and then it's assigning the value to this item.

And then finally, create default.php layout file inside your tmpl folder inside the place view and add the following line of code

  1. <?php defined( '_JEXEC' ) or die; ?>
  2. <h1 id="place_name"><?php echo $this->escape($this->item->place_name); ?></h1>
  3.  
  4. <div id="place">
  5. <div id="place_left">
  6. <p id="place_description">
  7. <?php echo $this->item->place_description; ?>
  8. </p>
  9. </div>
  10. </div>
  1. <?php echo $this->escape($this->item->place_name); ?>
  2. ...
  3. ...
  4. ...
  5.  
  6. <p id="place_description">
  7. <?php echo $this->item->place_description; ?>
  8. </p>
  9.  

You will notice that there are two things being pulled from the item. We have the place_name and the place_description and both of these are just going to be output here in the layout.

So let's see all this in action. We are going to load the place view and load the record with id=1.

View Pulling the single record

So let's go to index.php, we are going to pull up the noidacity option for the noidacity component, we are going to do view=place & id=1. And you will notice, it pulls in the header of Pragati Maidan, and then it pulls in the description for the Pragati Maidan place.

So all we had to do was define a JTable class, have the model, load the JTable class, return it, and then pass it along to the view and then the view is able to output all of the HTML here. So whatever you want to display just a single record in Joomla, just use a JTable class along with your model and then assign it to the view.