Actions

API16

JTable

From Joomla! Documentation

Revision as of 17:41, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.

[Edit Descripton] 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

Defined in

libraries/joomla/database/table.php

Methods

Method name Description
__construct Object constructor to set table and key fields. In most cases this will be overridden by child classes to explicitly set the table and key fields for a particular database table.
getFields Get the columns from database table.
getTableName Method to get the database table name for the class.
getKeyName Method to get the primary key field name for the table.
getDbo Method to get the JDatabase connector object.
setDBO Method to set the JDatabase connector object.
setRules Method to set rules for the record.
getRules Method to get the rules for the record.
reset Method to reset class properties to the defaults set in the class definition. It will ignore the primary key as well as any private class properties.
bind Method to bind an associative array or object to the JTable instance.This method only binds properties that are publicly accessible and optionally takes an array of properties to ignore when binding.
load Method to load a row from the database by primary key and bind the fields to the JTable instance properties.
check Method to perform sanity checks on the JTable instance properties to ensure they are safe to store in the database. Child classes should override this method to make sure the data they are storing in the database is safe and as expected before storage.
store Method to store a row in the database from the JTable instance properties. If a primary key value is set the row with that primary key value will be updated with the instance property values. If no primary key value is set a new row will be inserted into the database with the properties from the JTable instance.
save Method to provide a shortcut to binding, checking and storing a JTable instance to the database table. The method will check a row in once the data has been stored and if an ordering filter is present will attempt to reorder the table rows based on the filter. The ordering filter is an instance property name. The rows that will be reordered are those whose value matches the JTable instance for the property specified.
delete Method to delete a row from the database table by primary key value.
checkOut Method to check a row out if the necessary properties/fields exist. To prevent race conditions while editing rows in a database, a row can be checked out if the fields 'checked_out' and 'checked_out_time' are available. While a row is checked out, any attempt to store the row by a user other than the one who checked the row out should be held until the row is checked in again.
checkIn Method to check a row in if the necessary properties/fields exist. Checking a row in will allow other users the ability to edit the row.
hit Method to increment the hits for a row if the necessary property/field exists.
isCheckedOut TODO: This either needs to be static or not.
getNextOrder Method to get the next ordering value for a group of rows defined by an SQL WHERE clause. This is useful for placing a new item last in a group of items in the table.
reorder Method to compact the ordering values of rows in a group of rows defined by an SQL WHERE clause.
move Method to move a row in the ordering sequence of a group of rows defined by an SQL WHERE clause. Negative numbers move the row up in the sequence and positive numbers move it down.
publish Method to set the publishing state for a row or list of rows in the database table. The method respects checked out rows by other users and will attempt to checkin rows that it can after adjustments are made.
canDelete Generic check for whether dependancies exist for this object in the database schema
toXML Method to export the JTable instance properties to an XML string.
getInstance Static method to get an instance of a JTable class if it can be found in the table include paths. To add include paths for searching for JTable classes JTable::addIncludePath().
addIncludePath Add a filesystem path where JTable should search for table class files. You may either pass a string or an array of paths.

Importing

jimport( 'joomla.database.table' );

[Edit See Also] SeeAlso:JTable

Examples

<CodeExamplesForm />

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