MVC architecture is one of the most common and popular architectures for software developments today, especially for web applications.
Then I come across the Joomla MVC architecture, the way Joomla use MVC is a little bit different from what I used to have in JAVA, and the most difficult part is there are some hidden “Magical” links that make the whole MVC API work.
1. Terms and definition
- There are a few major players in the Joomla MVC API namely:
- JTable
- JModel
- JView
- JController
- JFactory
Let us look at their use one by one.
1.1 JTable
Strictly speaking, we can consider JTable not part of the Joomla MVC API. In fact, it existed since Joomla 1.0, long before Joomla MVC came out. And it is not even in the same package folder, being under “database folder”, instead of “application” folder where JModel, JView and JController are placed.
But why would I mention it here? First of all, from my opinion, JTable is a closer resemble of what we call “Model” in java programming, which model is a direct mapping of a database item. Secondly, most of the actual data manipulation operations in Joomla MVC are done through JTable.
What to do with JTable
There are 3 private variables in JTable:
- $_tbl – The Table Name
- $_tbl_key – Primary Key Column name
- $_db – Database connector
To use JTable, we must extend it first, create a file having a special name, for example:
| Table | File Name | Class Name |
| STUDENT | student.php | TableStudent |
| EXAM | exam.php | TableExam |
The reason for this special naming convention is, as i would show later, Joomla is doing some magic behind the scene. Place the php file in the “administrator/com_YOURCOMPONENT/tables” folder.
Then populate it with the fields that we want want this class to have, in other words, what are the database columns need to be mapped to this class.
Then create a constructor that tells the JTable what is the table name and primary key as shown below:
function __construct(&$db)
{
parent::__construct( '#__recipes', 'id', $db );
}
The next step , is to tell Joomla include the Class file, this should be usually done in the component entry point:
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_YOURCOMPONENT'.DS.'tables');
Why is it useful
To understand why is it useful, let’s look at some methods it provides:
- bind
- check
- store
- load
- delete
- reset
- Bind would copy the data from a associative array (hash map) or and object (stdobj) to the current JTable instance. This is just like we use BeanUtils in java to copy a web form to a POJO.
Check is the method should always be overridden unless nothing need to be checked. Although the check function is not normally automatically called, but it gives us a good entry point where all the validation code should go. Joomla usually use this method to populate fields using default formula if left empty.
Store is the method where the data is actually being persisted into the database.
Load – self explained
delete – self explained
reset – clear all the data fields except the primary key
By using these a few methods, all the common single item database operation can be achieved.
But that is not all yet, let’s look at the following methods, they will make your life much better as they provide quite some joomla specific common functions:
checkin/checkout / isCheckedOut– the joomla checkin checkout function if the database table supports that column
publish / unpublish – The publish / unpublish function
order / getNextOrder / reorder / Move – the ordering functions
hit – hit counter increment
save – different from store. The save function actually calls the following functions in order, and fails if any of them failed:
- bind
- check
- store
- checkin
- reorder (if supported)





