Using the JTable class

From Joomla! Documentation
(Difference between revisions)
Jump to: navigation, search
(New page: {{inuse}})
 
Line 1: Line 1:
 
{{inuse}}
 
{{inuse}}
 +
 +
 +
== Basics ==
 +
 +
The JTable class is an implementation of the [http://en.wikipedia.org/wiki/Active_record_pattern Active Record] design pattern. It is used throughout Joomla! for [http://en.wikipedia.org/wiki/Create,_read,_update_and_delete creating, reading, updating, and deleting] records in the database table.
 +
 +
To use JTable, create an extension of the class. In this example, we have a database table containing recipes.
 +
 +
<pre>
 +
class TableRecipes extends JTable
 +
{
 +
var $id = null;
 +
var $ingredients = null;
 +
var $instructions = null;
 +
var $serves = null;
 +
var $difficulty = null;
 +
var $prep_time = null;
 +
var $cook_time = null;
 +
var $published = 0;
 +
 +
function __construct(&$db)
 +
{
 +
parent::__construct( '#__recipes', 'id', $db );
 +
}
 +
}
 +
</pre>
 +
 +
When naming your class extension, the convention is to prefix it with 'Table', then follow with a [http://en.wikipedia.org/wiki/CamelCase CamelCased] version of the table's name. All of the member variables of your class should match the column names in the database. The default values should be valid according to the table schema For instance, if you have columns that are NOT NULL, you must use a value other than 'null' as the default.
 +
 +
Finally,  create a constructor for the class that accepts a reference to the current database instance. This will call the parent constructor which needs the name of the table, the name of the primary key, and the database instance. The name of the table uses #__ instead of jos_, as the administrator can pick any table prefix desired during Joomla! installation.

Revision as of 14:39, 19 January 2008


Basics

The JTable class is an implementation of the Active Record design pattern. It is used throughout Joomla! for creating, reading, updating, and deleting records in the database table.

To use JTable, create an extension of the class. In this example, we have a database table containing recipes.

class TableRecipes extends JTable
{
	var $id = null;
	var $ingredients = null;
	var $instructions = null;
	var $serves = null;
	var $difficulty = null;
	var $prep_time = null;
	var $cook_time = null;
	var $published = 0;
	
	function __construct(&$db)
	{
		parent::__construct( '#__recipes', 'id', $db );
	}
}

When naming your class extension, the convention is to prefix it with 'Table', then follow with a CamelCased version of the table's name. All of the member variables of your class should match the column names in the database. The default values should be valid according to the table schema For instance, if you have columns that are NOT NULL, you must use a value other than 'null' as the default.

Finally, create a constructor for the class that accepts a reference to the current database instance. This will call the parent constructor which needs the name of the table, the name of the primary key, and the database instance. The name of the table uses #__ instead of jos_, as the administrator can pick any table prefix desired during Joomla! installation.

Personal tools
Namespaces

Variants
Actions
Navigation
Joomla! Sites
Toolbox