The Joomla MVC classes
Written by Administrator   
Wednesday, 03 March 2010
Article Index
The Joomla MVC classes
The MVC calling sequence



Of the three base classes of the Joomla MVC pattern the JModel class is perhaps the least interesting because the tasks that the model has to perform are so various they are not really capable of generalisation. However the base class does provide a range of database access and manipulation methods which can be expanded upon in the derived class.

The base constructor for JModel accepts a configuration array which can specify the name of the view, the model state (more of which later), the database in use and a search path for database JTable objects.



The central database method is probably getDBO which returns the database connector object which can be used in queries etc. You can also set the database connector object using setDBO(). Working with the database is either a matter of working with the database object's own methods or you can make use of the slightly higher level methods provided by the model.

For example:


will retrieve $number records from the database that satisfy the query from the record specified by $start. You can find out how many records there are using:


Both of these methods use the more basic database object methods setQuery, query and loadObjectList. For example, to retrieve an array of objects each corresponding to a database row you might use:

$query = $this->_buildQuery();

where it is assumed that _buildQuery is a method that returns a valid SQL query.

In most cases you can do a better job by working with JTable derived objects. How to use JTable objects is the subject of another article. However to retrieve an instance of a JTable object you can use:


which will return the default JTable object or you can specify its name, prefix and an array of options. You can add to the paths where tables are searched for using addTablePath. In most cases it is desirable to use a JTable object to access the database but sometimes this isn't flexible enough and you need to work with the raw database connector object and a query.


If you need to create an instance of another Model class then you can use the getInstance method. The getInstance method is usually called as a static method:


Unlike other get methods it always creates a new instance of the Model. For example to create a new instance of the default model you would use:


which would create an instance of mycomponentModelmycomponent.

This method is only really useful if you want to load something other than the default model. You can also set additional paths where models are searched for using the addIncludePath() method.

The state object

The basic JModel class also includes the ability to use a state object but this isn't much used, mainly because Joomla models tend to be relatively simple. A state object is simply an object which is used to store properties which represent the state of the model. If you don't specify a state object within the constructor then an instance of JObject is used. You can create your own state object if you want to, derived from JObject even, but in many cases it is easier use JObject's ability to add properties dynamically. For example:


This first retrieves the default state object and then uses its set method to create a new property called dataloaded which is set to true.

You could have achieved the same result using:


or you could use the setState method provided by the model:


which is probably the easiest way of doing the job.

From this point on you can access the dataloaded property directly as in:


or you can use one of the state access methods that the model provides.

For example the getState method will return the value of a property, if one is specified, and the entire state object otherwise. For example


returns the current value of the dataloaded property and is equivalent to:


Clearly the setState and getState methods provided by the model are the simplest way of working with the state object. Using a state object to record the current state of the model is a good design choice - but not one much used in practice.






Last Updated ( Wednesday, 03 March 2010 )