J1.5

Difference between revisions of "Using the JTable class"

From Joomla! Documentation

Line 2: Line 2:
  
  
== Basics ==
+
== Writing an extension of JTable ==
  
 
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.
 
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.
Line 9: Line 9:
  
 
<pre>
 
<pre>
 +
<?php
 +
 +
defined('_JEXEC') or die();
 +
 
class TableRecipes extends JTable
 
class TableRecipes extends JTable
 
{
 
{
Line 29: Line 33:
 
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.
 
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.
+
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 column, 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.
 +
 
 +
If you were using this class as a part of a component called 'Recipes', you would place this code in the file /administrator/components/com_recipes/tables/recipes.php.
 +
 
 +
 
 +
== Using a JTable class extension ==
 +
 
 +
Once the table class is in place, you can use it in any Joomla! extension. To include the file, place this line in your extension's source code (use com_nameofyourcomponent in place of com_recipes):
 +
 
 +
<pre>
 +
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_recipes'.DS.'tables');
 +
</pre>
 +
 
 +
 
 +
=== Create/Update ===
 +
=== Read ===
 +
=== Delete ===
 +
 
 +
== Member Functions ==
 +
=== getInstance ===
 +
=== getDBO ===
 +
=== setDBO ===
 +
=== getTableName ===
 +
=== getKeyName ===
 +
=== reset ===
 +
=== bind ===
 +
=== load ===
 +
=== check ===
 +
=== store ===
 +
=== move ===
 +
=== getNextOrder ===
 +
=== reorder ===
 +
=== canDelete ===
 +
=== delete ===
 +
=== checkout ===
 +
=== checkin ===
 +
=== hit ===
 +
=== isCheckedOut ===
 +
=== save ===
 +
=== publish ===
 +
=== toXML ===
 +
=== addIncludePath ===
 +
== Summary ==

Revision as of 15:20, 19 January 2008

The "J1.5" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

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}}.


Writing an extension of JTable[edit]

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.

<?php

defined('_JEXEC') or die();

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 column, 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.

If you were using this class as a part of a component called 'Recipes', you would place this code in the file /administrator/components/com_recipes/tables/recipes.php.


Using a JTable class extension[edit]

Once the table class is in place, you can use it in any Joomla! extension. To include the file, place this line in your extension's source code (use com_nameofyourcomponent in place of com_recipes):

JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_recipes'.DS.'tables');


Create/Update[edit]

Read[edit]

Delete[edit]

Member Functions[edit]

getInstance[edit]

getDBO[edit]

setDBO[edit]

getTableName[edit]

getKeyName[edit]

reset[edit]

bind[edit]

load[edit]

check[edit]

store[edit]

move[edit]

getNextOrder[edit]

reorder[edit]

canDelete[edit]

delete[edit]

checkout[edit]

checkin[edit]

hit[edit]

isCheckedOut[edit]

save[edit]

publish[edit]

toXML[edit]

addIncludePath[edit]

Summary[edit]