Source code for file /joomla/database/table.php
Documentation is available at table.php
* @version $Id: table.php 6761 2007-03-03 07:09:23Z tcp $
* @package Joomla.Framework
* @copyright Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
// Check to ensure this file is within the rest of the framework
* Parent classes to all tables.
* @package Joomla.Framework
* @tutorial Joomla.Framework/jtable.cls
* Name of the table in the db schema relating to child class
* Name of the primary key field in the table
* Object constructor to set table and key field
* Can be overloaded/supplemented by the child class
* @param string $table name of the table in the db schema relating to child class
* @param string $key name of the primary key field in the table
* @param object $db JDatabase object
* Returns a reference to the a Table object, always creating it
* @param type $type The table type to instantiate
* @param string A prefix for the table class name
* @return database A database object
$tableClass =
$prefix.
ucfirst($type);
$instance =
new $tableClass($db);
* Get the internal database object
* @return object A JDatabase based object
* Set the internal database object
* @param object $db A JDatabase based object
* Gets the internal table name for the object
* Gets the internal primary key name
* Returns the error message
* Returns the error number
* @return int The error number
* Resets the default properties
if (($name !=
$k) and ($name !=
'_db') and ($name !=
'_tbl') and ($name !=
'_tbl_key')) {
* Binds a named array/hash to this object
* Can be overloaded/supplemented by the child class
* @param $from mixed An associative array or object
* @param $ignore mixed An array or space separated list of fields not to bind
function bind( $from, $ignore=
array() )
if (!$fromArray &&
!$fromObject)
// internal attributes of an object are ignored
if ($fromArray && isset
( $from[$k] )) {
} else if ($fromObject && isset
( $from->$k )) {
* Loads a row from the database and binds the fields to the object properties
* @param mixed Optional primary key. If not specifed, the value of current key is used
* @return boolean True if successful
function load( $oid=
null )
.
' WHERE '.
$this->_tbl_key.
' = '.
$db->Quote($oid);
if ($result =
$db->loadAssoc( )) {
return $this->bind($result);
* Can be overloaded/supplemented by the child class
* @return boolean True if the object is ok
* Inserts a new row if id is zero or updates an existing row in the database table
* Can be overloaded/supplemented by the child class
* @param boolean If false, null object variables are not updated
* @return null|stringnull if successful otherwise returns and error message
function store( $updateNulls=
false )
function move( $dirn, $where=
'' )
$sql .=
' WHERE ordering < '.
$this->ordering;
$sql .=
($where ?
' AND '.
$where :
'');
$sql .=
' ORDER BY ordering DESC';
$sql .=
' WHERE ordering > '.
$this->ordering;
$sql .=
($where ?
' AND '.
$where :
'');
$sql .=
' ORDER BY ordering';
$sql .=
'WHERE ordering = '.
$this->ordering;
$sql .=
($where ?
' AND '.
$where :
'');
$sql .=
' ORDER BY ordering';
$query =
'UPDATE '.
$this->_tbl
.
' SET ordering = "'.
$row->ordering.
'"'
.
' WHERE '.
$this->_tbl_key .
' = "'.
$this->$k .
'"'
$query =
'UPDATE '.
$this->_tbl
.
' SET ordering = "'.
$this->ordering.
'"'
.
' WHERE '.
$this->_tbl_key.
' = \''.
$row->$k.
'\''
$this->ordering =
$row->ordering;
$query =
'UPDATE '.
$this->_tbl
.
' SET ordering = "'.
$this->ordering.
'"'
.
' WHERE '.
$this->_tbl_key .
' = "'.
$this->$k .
'"'
* Returns the ordering value to place a new item last in its group
* @param string query WHERE clause for selecting MAX(ordering).
$query =
'SELECT MAX(ordering)' .
($where ?
' WHERE '.
$where :
'');
* Compacts the ordering sequence of the selected records
* @param string Additional where query to limit ordering to a particular subset of records
if ($this->_tbl ==
'#__content_frontpage')
$order2 =
", content_id DESC";
$query =
'SELECT '.
$this->_tbl_key.
', ordering'
.
( $where ?
' WHERE '.
$where :
'' )
.
' ORDER BY ordering'.
$order2
// compact the ordering numbers
for ($i=
0, $n=
count( $orders ); $i <
$n; $i++
)
if ($orders[$i]->ordering >=
0)
if ($orders[$i]->ordering !=
$i+
1)
$orders[$i]->ordering =
$i+
1;
$query =
'UPDATE '.
$this->_tbl
.
' SET ordering = "'.
$orders[$i]->ordering .
'"'
.
' WHERE '.
$k .
' = "'.
$orders[$i]->$k .
'"'
// for ($i=0; $i < $n; $i++)
// //echo "i=$i id=".$orders[$i]->$k." order=".$orders[$i]->ordering;
// if ($orders[$i]->$k == $this->$k)
// // place 'this' record in the desired location
// $orders[$i]->ordering = min( $this->ordering, $n );
// else if ($orders[$i]->ordering >= $this->ordering && $this->ordering > 0)
// $orders[$i]->ordering++;
// //echo '<pre>';print_r($orders);echo '</pre>';
// // compact once more until I can find a better algorithm
// for ($i=0, $n=count( $orders ); $i < $n; $i++)
// if ($orders[$i]->ordering >= 0)
// $orders[$i]->ordering = $i+1;
// $query = "UPDATE $this->_tbl"
// . "\n SET ordering = '". $orders[$i]->ordering ."'"
// . "\n WHERE $k = '". $orders[$i]->$k ."'"
// $this->_db->setQuery( $query);
// //echo '<br />'.$this->_db->getQuery();
// // if we didn't reorder the current record, make it last
// $query = "UPDATE $this->_tbl"
// . "\n SET ordering = '$order'"
// . "\n WHERE $k = '". $this->$k ."'"
// $this->_db->setQuery( $query );
// //echo '<br />'.$this->_db->getQuery();
* Generic check for whether dependancies exist for this object in the db schema
* can be overloaded/supplemented by the child class
* @param string $msg Error message returned
* @param int Optional key index
* @param array Optional array to compiles standard joins: format [label=>'Label',name=>'table name',idfield=>'field',joinfield=>'field']
foreach( $joins as $table )
$select .=
', COUNT(DISTINCT '.
$table['idfield'].
') AS '.
$table['idfield'];
$join .=
' LEFT JOIN '.
$table['name'].
' ON '.
$table['joinfield'].
' = '.
$k;
$query =
'SELECT '.
$select
.
' WHERE '.
$k .
' = "'.
$this->$k .
'"'
foreach( $joins as $table )
$msg[] =
JText::_( $table['label'] );
* can be overloaded/supplemented by the child class
* @return true if successful otherwise returns and error message
//if (!$this->canDelete( $msg ))
* @param integer The id of the user
* @param mixed The primary key value for the row
* @return boolean True if successful, or if checkout is not supported
$time =
date( 'Y-m-d H:i:s' );
' SET checked_out = '.(int)
$who.
', checked_out_time = '.
$this->_db->Quote($time) .
$this->checked_out =
$who;
$this->checked_out_time =
$time;
* @param mixed The primary key value for the row
* @return boolean True if successful, or if checkout is not supported
$this->checked_out_time =
'';
function hit( $oid=
null, $log=
false )
$query =
'UPDATE '.
$this->_tbl
.
' SET hits = ( hits + 1 )'
* Check if an item is checked out
* This function can be used as a static function too, when you do so you need to also provide the
* a value for the $against parameter.
* @param integer $with The userid to preform the match with, if an item is checked out
* by this user the function will return false
* @param integer $against The userid to perform the match against when the function is used as
$against =
$this->get( 'checked_out' );
//item is not checked out, or being checked out by the same user
if (!$against ||
$against ==
$with) {
return $session->exists($against);
* @param array Source array for binding to class vars
* @param string Filter for the order updating
* @returns TRUE if completely successful, FALSE if partially or not succesful.
function save( $source, $order_filter=
'' )
if (!$this->bind( $source ))
$filter_value =
$this->$order_filter;
* Sets the internal error message
* @param string The error message
* Sets the internal error number
* @param int Set the error number with this value
* Generic Publish/Unpublish function
* @param array An array of id numbers
* @param integer 0 if unpublishing, 1 if publishing
* @param integer The id of the user performnig the operation
function publish( $cid=
null, $publish=
1, $user_id=
0 )
$user_id = (int)
$user_id;
$publish = (int)
$publish;
$cids =
$k .
'=' .
implode( ' OR ' .
$k .
'=', $cid );
$query =
'UPDATE '.
$this->_tbl
.
' SET published = ' . (int)
$publish
$query .=
' AND (checked_out = 0 OR checked_out = '.
$user_id.
')';
if (count( $cid ) ==
1 &&
$checkin)
* Export item list to xml
* @param boolean Map foreign keys to text values
function toXML( $mapKeysToText=
false )
$xml =
'<record table="' .
$this->_tbl .
'"';
$xml .=
' mapkeystotext="true"';
$xml .=
'<' .
$k .
'><![CDATA[' .
$v .
']]></' .
$k .
'>';
* Add a directory where JTable should search for table types. You may
* either pass a string or an array of directories.
* @param string A path to search.
* @return array An array with directory elements
$paths =
array( JPATH_LIBRARIES.
DS.
'joomla'.
DS.
'database'.
DS.
'table' );
// just force path to array
if (!empty( $path ) &&
!in_array( $path, $paths ))
// loop through the path directories
// no surrounding spaces allowed!
// add to the top of the search dirs
//array_unshift($paths, $dir);