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

 

JModel

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.

Banner

Database

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:

_getList($query,$start,$number);

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:

_getListCount($query)/

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();
$this->_data=$this->_getList($query);

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:

getTable()

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.

Models

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:

JModel::getInstance($type);

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:

$model=JModel::getInstance(
'MyComponent','mycomponentModel');

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:

$mystate=$this->getState();
$mystate->set('dataloaded',true);

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:

$this->getState()->
set('dataloaded',true);

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

$this->setState('dataloaded',true);

which is probably the easiest way of doing the job.

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

$mystate->dataloaded=false;

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

$temp=getState('dataloaded');

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

$temp=getState->get('dataloaded');
$temp=getState()->dataloaded;

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.

Banner

<ASIN:1847197701>

<ASIN:1598638149>

<ASIN:1847196748>

<ASIN:047057089X>



Last Updated ( Wednesday, 03 March 2010 )