Xtremax - Web Design

17 Mar, 2009

Inside Joomla MVC

Posted by: Mr Cheetah In: test category

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

image

publish / unpublish – The publish / unpublish function

image

order / getNextOrder / reorder / Move – the ordering functions

image

hit – hit counter increment

image

savedifferent from store. The save function actually calls the following functions in order, and fails if any of them failed:

  1. bind
  2. check
  3. store
  4. checkin
  5. reorder (if supported)
Bookmark and Share

7 Responses to "Inside Joomla MVC"

1 | Peter

June 10th, 2009 at 7:02 pm

Avatar

I think it is interesting to demonstrate how we can populate such an object derived from JTable from within a model:

$student =& $this->getTable('student');
$student->load($student_id);
return $student;

2 | Kei

June 25th, 2009 at 9:59 am

Avatar

Great article! Please keep writing on Joomla MVC development.
I'm looking forward to reading it.

4 | Asd

July 4th, 2010 at 4:03 pm

Avatar

We offer new-style christian louboutin,Fashion and discount Christian Louboutin Shoes platform with best quality and no tax shipping.Our mission is to provide the highest quality with lowest price.Goods and merchandise sold both in person or online are subject to the same quality assurance agreement.We are committed to assuring customer satisfaction.Clients will be expected to accept any fees.We thank you our valued customer for your current and future business We sell christianlouboutinboots,discount christian louboutin Sale,Giuseppe Zanotti Shoes sandals at All the
louboutin shoes are brand new and beautiful. We have taken many photoes of christian louboutin shoes in details Herve Leger Herve Leger
Herve Leger Buy quality Moncler Jacket Vest with best after sale service at Stylemoncler.com. You can purchase Moncler Vest which are selected strictly by our high level quality control team. We only sell genuine Moncler Jackets Vest for Men's, Women's and Kids with competitive price. We provide free shipping service for all Moncler Jackets also including Moncler Vest Down Jackets to worldwide! We believe that you will find your favorite and suitable Moncler Down Vest from us. Hurry up and place your order now.
Herve Leger

5 | ugg boots

July 7th, 2010 at 1:43 pm

Avatar

Here elaborates the matter not only extensively but also detailly .I support the
write's unique point.It is useful and new balance a20 Navajo benefit to your daily life.You can go those
sits to know more relate things.They are strongly recommended by friends.Personally

6 | bape

July 7th, 2010 at 2:53 pm

Avatar

Hhe article's content rich variety which make us move for our mood after reading this article. surprise, here you will find what you want! Recently, I found some wedsites which commodity is colorful of fashion. Such as that worth you to see. Believe me these websites wont let you down. ed hardy stores

7 | Kobe Bryant Shoes

July 14th, 2010 at 1:57 pm

Avatar

I know a new method to decorate the house from the post.Not only simple,but also generous. Dior high-heeled shoesDon't let a person feel extravagant. I recommend several websites to you that contents are very rich you can go and see . Nike Zoom LeBron James Soldier

Comment Form

Xtremax Web Design Portfolio

    Opera Estate Primary SchoolNational Association of Travel Agents SingaporeFUMIYAMA CORPORATIONEngineering Education PortalBOON HI-TECH SUPERSTORESingapore Tourism Board

About Xtremax

This is all about a group of hard-core web developers who go about rambling about their daily efforts to make the internet world a better and more accessible place.