📄 ktentity.inc.svn-base
字号:
<?php/** * $Id$ * * Base class for database-backed objects * * KnowledgeTree Community Edition * Document Management Made Simple * Copyright (C) 2008 KnowledgeTree Inc. * Portions copyright The Jam Warehouse Software (Pty) Limited * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 3 as published by the * Free Software Foundation. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, * California 94120-7775, or email info@knowledgetree.com. * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU General Public License version 3. * * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "Powered by * KnowledgeTree" logo and retain the original copyright notice. If the display of the * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices * must display the words "Powered by KnowledgeTree" and retain the original * copyright notice. * Contributor( s): ______________________________________ */$_LCASECACHE = array();$_OBJECTCACHE = array();$_RANDOMCALL = 0;$_STOPCACHING = array();require_once(KT_LIB_DIR . '/cache/cache.inc.php');DEFINE("EVIL_CACHE_GRIND", false);class KTEntity { var $_bUsePearError = false; /** object primary key */ var $iId = -1; function getId() { return $this->iId; } function setId($mValue) { $this->iId = $mValue; } function _cachedGroups() { return array('getlist'); } function _innerClearCachedGroups($sClassName) { if ($GLOBALS['_STOPCACHING'][$sClassName]) { if ($GLOBALS['_STOPCACHING'][$sClassName] > 5) { return; } } else { $GLOBALS['_STOPCACHING'][$sClassName] = 0; } $group_func = array($sClassName, '_cachedGroups'); if (is_callable($group_func)) { $groups = call_user_func($group_func); } else { $groups = array('getlist'); } $groups[] = 'auto'; $oCache =& KTCache::getSingleton(); $aSuffixes = array(''); // , '_count', '_fullselect'); foreach ($groups as $group_base) { global $default; foreach ($aSuffixes as $sSuffix) { $group = sprintf("%s/%s%s", $sClassName, $group_base, $sSuffix); if (KTLOG_CACHE) $default->log->debug("Clearing cached group: $group"); $oCache->clear($group); } } $GLOBALS['_STOPCACHING'][$sClassName]++; } function clearCachedGroups() { $sClass = get_class($this); $this->_innerClearCachedGroups($sClass); } /** * Create the current object in the database * * @return boolean on successful store, false otherwise and set $_SESSION["errorMessage"] * */ function create() { if ($this->iId <= 0) { $id = DBUtil::autoInsert($this->_table(), $this->_fieldValues()); if (PEAR::isError($id)) { if ($this->_bUsePearError === false) { $_SESSION["errorMessage"] = $id->toString(); return false; } else { return $id; } } $this->clearCachedGroups(); $this->iId = $id; return true; } $_SESSION["errorMessage"] = "Can't create an object that already exists id = " . $this->iId . ' table = ' . $this->_table(); return false; } function newCopy() { $sClass = get_class($this); $oObject =new $sClass; foreach (array_keys($this->_aFieldToSelect) as $k) { $oObject->$k = $this->$k; } $oObject->iId = -1; $oObject->create(); $iId = $oObject->iId; return KTEntityUtil::get($sClass, $iId); } /** * Update the values in the database table with the object's current values * * @return boolean true on successful update, false otherwise and set $_SESSION["errorMessage"] * */ function update() { $group = sprintf("%s/%s", get_class($this), 'id'); $oCache =& KTCache::getSingleton(); if ($oCache->get($group, $this->iId) != array(false, false)) { $oCache->remove($group, $this->iId); } $this->clearCachedGroups(); if ($this->iId > 0) { $res = DBUtil::autoUpdate($this->_table(), $this->_fieldValues(), $this->iId); if (PEAR::isError($res)) { if ($this->_bUsePearError === false) { $_SESSION['errorMessage'] = $res->toString(); return false; } return $res; } return true; } $_SESSION["errorMessage"] = "Can't update an object that isn't in the database"; return false; } /** * Delete the current object from the database * * @return boolean true on successful deletion, false otherwise and set $_SESSION["errorMessage"] * */ function delete($dereference = null) { $group = sprintf("%s/%s", get_class($this), 'id'); $oCache =& KTCache::getSingleton(); $oCache->remove($group, $this->iId); $this->clearCachedGroups(); if ($this->iId >= 0) { if($dereference){ $res = DBUtil::deReference($this->_table(), $this->iId); }else{ $res = DBUtil::autoDelete($this->_table(), $this->iId); } if (PEAR::isError($res)) { if ($this->_bUsePearError === false) { $_SESSION['errorMessage'] = $res->toString(); return false; } else { return $res; } } return true; } $_SESSION["errorMessage"] = "Can't delete an object that isn't in the database";; return false; } function _getSqlSelection() { $aRet = array(); foreach ($this->_aFieldToSelect as $k => $v) { $aRet[] = $v; } return join(", ", $aRet); } function load($iId = null) { global $default; if (is_null($iId)) { if (is_null($this->iId)) { return PEAR::raiseError(_kt("No ID given")); } $iId = $this->iId; } $sClassName = get_class($this); // cache at lowest possible level. $oCache =& KTCache::getSingleton(); $group = sprintf("%s/%s", $sClassName , 'id'); list($bCached, $mCached) = $oCache->get($group, $iId); if ($bCached && EVIL_CACHE_GRIND) { if (KTLOG_CACHE) $default->log->debug(sprintf(_kt("Found and testing object cache for class %s, id %d"), $sClassName, $iId)); $table = $this->_table(); $select = $this->_getSqlSelection(); $sQuery = "SELECT $select FROM $table WHERE id = ?"; $aParams = array($iId); $res = DBUtil::getResultArray(array($sQuery, $aParams)); // we _have_ a cache: error is a disaster if (PEAR::isError($res) || (count($res) === 0) || (count($res) > 1)) { $oCache->alertFailure($sClassName, array('<strong>SERIOUS FAILURE:</strong> real object is an error, cache claims "valid".')); return $res; } // now compare the sub-values. $aFailures = array(); $sEntClass = get_class($this); foreach ($res as $sKey => $aVal) { if ($mCached[$sKey] != $res[$sKey]) { $id = $aVal['id']; foreach ($aVal as $sField => $sStored) { if ($mCached[$sKey][$sField] != $sStored) { $aFailures[] = sprintf("For %d field %s, stored value is %s, but cached value is %s", $id, $sField, $sStored, $mCached[$sKey][$sField]); } } // $aFailures[] = $sKey; } } if (!empty($aFailures)) { $oCache->alertFailure($sClassName, $aFailures); } $res = $mCached; } else if ($bCached) { global $default; if (KTLOG_CACHE) $default->log->debug(sprintf("Using object cache for class %s, id %d", $sClassName, $iId)); $res = $mCached; /* */ } else { if (KTLOG_CACHE) $default->log->debug(sprintf("No object cache for class %s, id %d", $sClassName, $iId)); $table = $this->_table(); $select = $this->_getSqlSelection(); $sQuery = "SELECT $select FROM $table WHERE id = ?"; $aParams = array($iId); $res = DBUtil::getResultArray(array($sQuery, $aParams)); if (PEAR::isError($res)) { return $res; } if (count($res) === 0) { return PEAR::raiseError(_kt("No such ID: ") . $iId); } if (count($res) > 1) { return PEAR::raiseError(_kt("Multiple matches for ID: ") . $iId); } $oCache->set($group, $iId, $res); // finally, cache if its "good". } $vk = array_flip($this->_aFieldToSelect); $aLoadInfo = array(); foreach ($res[0] as $k => $v) { $aLoadInfo[$vk[$k]] = $v; } $res = $this->loadFromArray($aLoadInfo); if (PEAR::isError($res)) { return $res; } } function loadFromArray ($aOptions) { if (!is_array($aOptions)) { return PEAR::raiseError(_kt("Expected an array!")); } foreach ($aOptions as $sField => $sValue) { $sElement = $this->_getElementFromMethod($sField); if ($sElement === false) { return PEAR::raiseError(_kt('Setting a non-existent field: ') . $sField); } if (PEAR::isError($sElement)) { return $sElement; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -