Source for file storage.php
Documentation is available at storage.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* Provides an object interface to a table row
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
* @author Stig Bakken <stig@php.net>
* @copyright 1997-2007 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: storage.php,v 1.24 2007/08/12 05:27:25 aharvey Exp $
* @link http://pear.php.net/package/DB
* Obtain the DB class so it can be extended from
require_once DB_PEAR_PATH.
'DB.php';
* Provides an object interface to a table row
* It lets you add, delete and change rows using objects rather than SQL
* @author Stig Bakken <stig@php.net>
* @copyright 1997-2007 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version Release: 1.7.13
* @link http://pear.php.net/package/DB
/** the name of the table (or view, if the backend database supports
updates in views) we hold data from */
/** which column(s) in the table contains primary keys, can be a
string for single-column primary keys, or an array of strings
for multiple-column primary keys */
/** DB connection handle used for all transactions */
/** an assoc with the names of database fields stored as properties
/** an assoc with the names of the properties in this object that
have been changed since they were fetched from the database */
/** flag that decides if data in this object can be changed.
objects that don't have their table's key column in their
property lists will be flagged as read-only. */
/** function or method that implements a validator for fields that
are set, this validator function returns true if the field is
* @param $table string the name of the database table
* @param $keycolumn mixed string with name of key column, or array of
* strings if the table has a primary key of more than one column
* @param $dbh object database connection object
* @param $validator mixed function or method used to validate
* each new value, called with three parameters: the name of the
* field/column that is changing, a reference to the new value and
* a reference to this object
function DB_storage($table, $keycolumn, &$dbh, $validator =
null)
* Utility method to build a "WHERE" clause to locate ourselves in
* XXX future improvement: use rowids?
function _makeWhere($keyval =
null)
// there's not much point in having a NULL key,
// but we support it anyway
$whereclause .=
' IS NULL';
$whereclause .=
' = ' .
$this->_dbh->quote($keyval[$i]);
// there's not much point in having a NULL key,
// but we support it anyway
$whereclause .=
' IS NULL';
$whereclause .=
' = ' .
$this->_dbh->quote($keyval);
* Method used to initialize a DB_storage object from the
* @param $keyval mixed the key[s] of the row to fetch (string or array)
* @return int DB_OK on success, a DB error if not
$whereclause =
$this->_makeWhere($keyval);
$query =
'SELECT * FROM ' .
$this->_table .
' WHERE ' .
$whereclause;
$sth =
$this->_dbh->query($query);
foreach ($row as $key =>
$value) {
* Create a new (empty) row in the configured table for this
for ($i =
0; $i <
sizeof($primarykey); $i++
) {
$pkvals[] =
$this->_dbh->quote($newpk[$i]);
$sth =
$this->_dbh->query("INSERT INTO $this->_table (" .
implode(",", $primarykey) .
") VALUES(" .
* Output a simple description of this DB_storage object.
* @return string object description
$info .=
" [loaded, key=";
for ($i =
0; $i <
sizeof($keyname); $i++
) {
$info .=
$this->$keyname[$i];
$info .=
$this->$keyname;
* Dump the contents of this object to "standard output".
* Static method used to create new DB storage objects.
* @param $data assoc. array where the keys are the names
* @return object a new instance of DB_storage or a subclass of it
function &create($table, &$data)
$obj =
new $classname($table);
foreach ($data as $name =>
$value) {
* Loads data into this object from the given query. If this
* object already contains table data, changes will be saved and
* the object re-initialized first.
* @param $query SQL query
* @param $params parameter list in case you want to use
* @return int DB_OK on success, DB_WARNING_READ_ONLY if the
* returned object is read-only (because the object's specified
* key column was not found among the columns returned by $query),
* or another DB error code in case of errors.
// XXX commented out for now
function loadFromQuery($query, $params = null)
if (sizeof($this->_properties)) {
if (sizeof($this->_changes)) {
$this->_changes = array();
$this->_properties = array();
$rowdata = $this->_dbh->getRow($query, DB_FETCHMODE_ASSOC, $params);
if (DB::isError($rowdata)) {
$found_keycolumn = false;
while (list($key, $value) = each($rowdata)) {
if ($key == $this->_keycolumn) {
$this->_properties[$key] = true;
unset($value); // have to unset, or all properties will
// refer to the same value
return DB_WARNING_READ_ONLY;
* Modify an attriute value.
function set($property, $newvalue)
// only change if $property is known and object is not
return $this->raiseError(null, DB_WARNING_READ_ONLY, null,
$this->$property =
$newvalue;
if (empty($this->_changes[$property])) {
null, "invalid field: $property",
null, "unknown field: $property",
* Fetch an attribute value.
* @param string attribute name
* @return attribute contents, or null if the attribute name is
// only return if $property is known
* Destructor, calls DB_storage::store() if there are changes
* Stores changes to this object in the database.
* @return DB_OK or a DB error
foreach ($this->_changes as $name =>
$foo) {
$params[] =
&$this->$name;
$vars[] =
$name .
' = ?';
$query =
'UPDATE ' .
$this->_table .
' SET ' .
$stmt =
$this->_dbh->prepare($query);
$res =
$this->_dbh->execute($stmt, $params);
* Remove the row represented by this object from the database.
* @return mixed DB_OK or a DB error
return $this->raiseError(null, DB_WARNING_READ_ONLY, null,
$query =
'DELETE FROM ' .
$this->_table .
' WHERE '.
$res =
$this->_dbh->query($query);
Documentation generated on Wed, 09 Feb 2011 09:04:47 +0700 by phpDocumentor 1.4.2