📄 table.php
字号:
<?php/** * @version $Id: table.php 11646 2009-03-01 19:34:56Z ian $ * @package Joomla.Framework * @subpackage Table * @copyright Copyright (C) 2005 - 2008 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 frameworkdefined('JPATH_BASE') or die();/** * Abstract Table class * * Parent classes to all tables. * * @abstract * @package Joomla.Framework * @subpackage Table * @since 1.0 * @tutorial Joomla.Framework/jtable.cls */class JTable extends JObject{ /** * Name of the table in the db schema relating to child class * * @var string * @access protected */ var $_tbl = ''; /** * Name of the primary key field in the table * * @var string * @access protected */ var $_tbl_key = ''; /** * Database connector * * @var JDatabase * @access protected */ var $_db = null; /** * Object constructor to set table and key field * * Can be overloaded/supplemented by the child class * * @access protected * @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 */ function __construct( $table, $key, &$db ) { $this->_tbl = $table; $this->_tbl_key = $key; $this->_db =& $db; } /** * Returns a reference to the a Table object, always creating it * * @param type $type The table type to instantiate * @param string $prefix A prefix for the table class name. Optional. * @param array $options Configuration array for model. Optional. * @return database A database object * @since 1.5 */ function &getInstance( $type, $prefix = 'JTable', $config = array() ) { $false = false; $type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type); $tableClass = $prefix.ucfirst($type); if (!class_exists( $tableClass )) { jimport('joomla.filesystem.path'); if($path = JPath::find(JTable::addIncludePath(), strtolower($type).'.php')) { require_once $path; if (!class_exists( $tableClass )) { JError::raiseWarning( 0, 'Table class ' . $tableClass . ' not found in file.' ); return $false; } } else { JError::raiseWarning( 0, 'Table ' . $type . ' not supported. File not found.' ); return $false; } } //Make sure we are returning a DBO object if (array_key_exists('dbo', $config)) { $db =& $config['dbo']; } else { $db = & JFactory::getDBO(); } $instance = new $tableClass($db); //$instance->setDBO($db); return $instance; } /** * Get the internal database object * * @return object A JDatabase based object */ function &getDBO() { return $this->_db; } /** * Set the internal database object * * @param object $db A JDatabase based object * @return void */ function setDBO(&$db) { $this->_db =& $db; } /** * Gets the internal table name for the object * * @return string * @since 1.5 */ function getTableName() { return $this->_tbl; } /** * Gets the internal primary key name * * @return string * @since 1.5 */ function getKeyName() { return $this->_tbl_key; } /** * Resets the default properties * @return void */ function reset() { $k = $this->_tbl_key; foreach ($this->getProperties() as $name => $value) { if($name != $k) { $this->$name = $value; } } } /** * Binds a named array/hash to this object * * Can be overloaded/supplemented by the child class * * @access public * @param $from mixed An associative array or object * @param $ignore mixed An array or space separated list of fields not to bind * @return boolean */ function bind( $from, $ignore=array() ) { $fromArray = is_array( $from ); $fromObject = is_object( $from ); if (!$fromArray && !$fromObject) { $this->setError( get_class( $this ).'::bind failed. Invalid from argument' ); return false; } if (!is_array( $ignore )) { $ignore = explode( ' ', $ignore ); } foreach ($this->getProperties() as $k => $v) { // internal attributes of an object are ignored if (!in_array( $k, $ignore )) { if ($fromArray && isset( $from[$k] )) { $this->$k = $from[$k]; } else if ($fromObject && isset( $from->$k )) { $this->$k = $from->$k; } } } return true; } /** * Loads a row from the database and binds the fields to the object properties * * @access public * @param mixed Optional primary key. If not specifed, the value of current key is used * @return boolean True if successful */ function load( $oid=null ) { $k = $this->_tbl_key; if ($oid !== null) { $this->$k = $oid; } $oid = $this->$k; if ($oid === null) { return false; } $this->reset(); $db =& $this->getDBO(); $query = 'SELECT *' . ' FROM '.$this->_tbl . ' WHERE '.$this->_tbl_key.' = '.$db->Quote($oid); $db->setQuery( $query ); if ($result = $db->loadAssoc( )) { return $this->bind($result); } else { $this->setError( $db->getErrorMsg() ); return false; } } /** * Generic check method * * Can be overloaded/supplemented by the child class * * @access public * @return boolean True if the object is ok */ function check() { return true; } /** * 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 * * @access public * @param boolean If false, null object variables are not updated * @return null|string null if successful otherwise returns and error message */ function store( $updateNulls=false ) { $k = $this->_tbl_key; if( $this->$k) { $ret = $this->_db->updateObject( $this->_tbl, $this, $this->_tbl_key, $updateNulls ); } else { $ret = $this->_db->insertObject( $this->_tbl, $this, $this->_tbl_key ); } if( !$ret ) { $this->setError(get_class( $this ).'::store failed - '.$this->_db->getErrorMsg()); return false; } else { return true; } } /** * Description * * @access public * @param $dirn * @param $where */ function move( $dirn, $where='' ) { if (!in_array( 'ordering', array_keys($this->getProperties()))) { $this->setError( get_class( $this ).' does not support ordering' ); return false; } $k = $this->_tbl_key; $sql = "SELECT $this->_tbl_key, ordering FROM $this->_tbl"; if ($dirn < 0) { $sql .= ' WHERE ordering < '.(int) $this->ordering; $sql .= ($where ? ' AND '.$where : ''); $sql .= ' ORDER BY ordering DESC'; } else if ($dirn > 0) { $sql .= ' WHERE ordering > '.(int) $this->ordering; $sql .= ($where ? ' AND '. $where : ''); $sql .= ' ORDER BY ordering'; } else { $sql .= ' WHERE ordering = '.(int) $this->ordering; $sql .= ($where ? ' AND '.$where : ''); $sql .= ' ORDER BY ordering'; } $this->_db->setQuery( $sql, 0, 1 ); $row = null; $row = $this->_db->loadObject(); if (isset($row)) { $query = 'UPDATE '. $this->_tbl . ' SET ordering = '. (int) $row->ordering . ' WHERE '. $this->_tbl_key .' = '. $this->_db->Quote($this->$k) ; $this->_db->setQuery( $query ); if (!$this->_db->query()) { $err = $this->_db->getErrorMsg(); JError::raiseError( 500, $err ); } $query = 'UPDATE '.$this->_tbl . ' SET ordering = '.(int) $this->ordering . ' WHERE '.$this->_tbl_key.' = '.$this->_db->Quote($row->$k) ; $this->_db->setQuery( $query ); if (!$this->_db->query()) { $err = $this->_db->getErrorMsg(); JError::raiseError( 500, $err ); } $this->ordering = $row->ordering; } else { $query = 'UPDATE '. $this->_tbl . ' SET ordering = '.(int) $this->ordering . ' WHERE '. $this->_tbl_key .' = '. $this->_db->Quote($this->$k) ; $this->_db->setQuery( $query ); if (!$this->_db->query()) { $err = $this->_db->getErrorMsg(); JError::raiseError( 500, $err ); } } return true; } /** * Returns the ordering value to place a new item last in its group * * @access public * @param string query WHERE clause for selecting MAX(ordering). */ function getNextOrder ( $where='' ) { if (!in_array( 'ordering', array_keys($this->getProperties()) )) { $this->setError( get_class( $this ).' does not support ordering' ); return false; } $query = 'SELECT MAX(ordering)' . ' FROM ' . $this->_tbl . ($where ? ' WHERE '.$where : ''); $this->_db->setQuery( $query ); $maxord = $this->_db->loadResult(); if ($this->_db->getErrorNum()) { $this->setError($this->_db->getErrorMsg()); return false; } return $maxord + 1; } /** * Compacts the ordering sequence of the selected records * * @access public * @param string Additional where query to limit ordering to a particular subset of records */ function reorder( $where='' ) { $k = $this->_tbl_key; if (!in_array( 'ordering', array_keys($this->getProperties() ) )) { $this->setError( get_class( $this ).' does not support ordering'); return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -