Actions

J1.5

Difference between revisions of "Using the JTable class"

From Joomla! Documentation

(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

Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.
Quill icon.png
Page Actively Being Edited!

This j1.5 page is actively undergoing a major edit for a short while.
As a courtesy, please do not edit this page while this message is displayed. The user who added this notice will be listed in the page history. This message is intended to help reduce edit conflicts; please remove it between editing sessions to allow others to edit the page. If this page has not been edited for several hours, please remove this template, or replace it with {{underconstruction}} or {{incomplete}}.


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.