JTable

From Joomla! Documentation
Jump to: navigation, search

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.

Contents

Availability

From Joomla 1.5 With additional functionality in Joomla 1.6

Defined in

/joomla/database/table.php

Extends

Extended by

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

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.

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.

Ordering

Many Joomla tables support a user-defined ordering of the rows.

To be written.

Publish/Unpublish

To be written.

Get and Set Methods

Properties which do not have specific get or set methods listed here can be retrieved or set using the inherited JObject->get method.

Get method Set method Description
getAssetNamePrefix Abstract method to return the name prefix to use for the asset table. From 1.6
getAssetSection Abstract method to return the access section name for the asset table. From 1.6
getAssetTitle Abstract method to return the title to use for the asset table. From 1.6
getDBO setDBO Method to set/get the JDatabase connector object associated with the table.
getTableName Method to get the name of the database table.
getTableKey Method to get the primary key field name for the table

Other Methods

Method name Description
construct Object constructor to set table and key field
addIncludePath Add a filesystem path where JTable should search for table class files.
bind Binds an associative array or object to the JTable instance.
canDelete Checks whether dependencies exist for this object in the database schema.
check Perform sanity checks on the JTable instance properties.
checkin Checks a row in if the necessary instance properties and table fields exist.
checkout Checks a row out if the necessary instance properties and table fields exist.
delete Deletes a row from the database table by primary key value.
getInstance Static method to get an instance of a JTable class.
getNextOrder Gets the next ordering value for a group of rows defined by an SQL WHERE clause.
hit Increments the hits for a row if the necessary instance property and table field exists.
isCheckedOut Determines if a row is checked out and therefore un-editable by a user.
load Loads a row from the database by primary key and binds the fields to the JTable instance properties.
move Moves a row in the ordering sequence of a group of rows defined by an SQL WHERE clause.
publish Sets the publishing state for a row or list of rows in the database table.
reorder Compacts the ordering values of rows in a group of rows defined by an SQL WHERE clause.
reset Resets class properties to the defaults set in the class definition.
save Provides a shortcut to binding, checking and storing a JTable instance to the database table.
store Stores a row in the database from the JTable instance properties.
toXML Exports the JTable instance properties to an XML string.

Importing

jimport( 'joomla.database.table' );

Extending the JTable Class for new database tables

The JTable Class can easily be extended in order to make the table object represent any table in your database. A good start how to learn about creating Child Classes for the JTable Class is to take a look at the fourth part of the MVC Tutorial

See also

Personal tools
Namespaces

Variants
Actions
Navigation
Joomla! Sites
Toolbox