API15

JTable

From Joomla! Documentation

The "API15" 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.

JTable is an abstract class that forms the basis for all database table classes. Some of the methods listed will be overridden by the child class so you should check the child class documentation for further information.

Each physical database table created should have a corresponding class derived from JTable to represent it. JTable provides many methods to make common manipulations to the table much simpler. For example, one of the most common operations you will need to perform is to read a table row into memory given a value for the primary key. This can be done easily using the load method. The table row can then be just as easily updated using the save method, which also performs any predefined sanity checks on the table fields.

Defined in[edit]

libraries/joomla/database/table.php

Methods[edit]

Method name Description
__construct Object constructor to set table and key field
getInstance Returns a reference to the a Table object, always creating it
getDBO Get the internal database object
setDBO Set the internal database object
getTableName Gets the internal table name for the object
getKeyName Gets the internal primary key name
reset Resets the default properties void
bind Binds a named array/hash to this object
load Loads a row from the database and binds the fields to the object properties
check Generic check method
store Inserts a new row if id is zero or updates an existing row in the database table
move Description
getNextOrder Returns the ordering value to place a new item last in its group
reorder Compacts the ordering sequence of the selected records
canDelete Generic check for whether dependancies exist for this object in the db schema
delete Default delete method
checkout Checks out a row
checkin Checks in a row
hit Description
save Generic save function
publish Generic Publish/Unpublish function
toXML Export item list to xml
addIncludePath Add a directory where JTable should search for table types. You may either pass a string or an array of directories.
isCheckedOut Check if an item is checked out

Importing[edit]

jimport( 'joomla.database.table' );



Examples[edit]

Code Examples[edit]

Automatically Generated Properties

From 2.5 As of Joomla 2.5, JTable properties are automatically generated based on the schema of the specified table. When you specify a table in a class that extends JTable, the field names are read from the table and set as properties. When using Joomla 1.5, you must manually declare all table columns as properties of the class that extends JTable.


Chris Davenport 12:21, 17 April 2011 (CDT) Edit comment

Reserved Database Field Names

Some of the optional features of JTable require the existence of specially-named fields in the database table. If you require this additional functionality you should ensure that these named fields are present in the table. These field names should be considered reserved as any attempts to use them for purposes other than those supported by JTable may result in conflict.

Field name Methods using the field name
checked_out checkOut, checkIn, isCheckedOut
checked_out_time checkOut, checkIn, isCheckedOut
hits hit
ordering getNextOrder, reorder, move
published publish


Chris Davenport 12:22, 17 April 2011 (CDT) Edit comment

Check-in/check-out

Joomla tables implement a simple mechanism for preventing a race condition while editing rows in a database. This depends on the existence of database fields called "checked_out" and "checked_out_time" and if these fields are present JTable will automatically support this mechanism so that it can be easily used in your tables too. In addition to the checkOut and checkIn methods, there is a isCheckedOut method to determine if a given table row is currently checked out by another user.


Chris Davenport 12:23, 17 April 2011 (CDT) Edit comment

Hit counter

Some Joomla tables contain a field called "hits" which records the number of times that a table row has been accessed. JTable provides a simple method to increment this field: hit.


Chris Davenport 12:24, 17 April 2011 (CDT) Edit comment