cvs: pear /DB_DataObject DataObject.php
| From: | Alan Knowles | Date: | Fri, 16 Aug 2002 05:15:55 +0000 |
| Subject: | cvs: pear /DB_DataObject DataObject.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-5768@lists.php.net to get a copy of this message | ||
alan_k Fri Aug 16 01:15:55 2002 EDT
Modified files:
/pear/DB_DataObject DataObject.php
Log:
bug fixes by Tim White
Index: pear/DB_DataObject/DataObject.php diff -u pear/DB_DataObject/DataObject.php:1.20 pear/DB_DataObject/DataObject.php:1.21 --- pear/DB_DataObject/DataObject.php:1.20 Thu Aug 15 01:13:51 2002 +++ pear/DB_DataObject/DataObject.php Fri Aug 16 01:15:53 2002 @@ -15,7 +15,7 @@ // | Author: Alan Knowles <alan@akbkhome.com> // +----------------------------------------------------------------------+ // -// $Id: DataObject.php,v 1.20 2002/08/15 05:13:51 alan_k Exp $ +// $Id: DataObject.php,v 1.21 2002/08/16 05:15:53 alan_k Exp $ // // Object Based Database Query Builder and data store // @@ -23,7 +23,7 @@ require_once 'PEAR.php'; /** - * these are constants for the get_table array + * these are constants for the get_table array * user to determine what type of escaping is required around the object vars. */ define("DB_DATAOBJECT_INT", 1); // does not require '' @@ -33,9 +33,9 @@ define("DB_DATAOBJECT_BOOL", 16); // is boolean #TODO define("DB_DATAOBJECT_TXT", 32); // is long text #TODO -/* +/* * Theses are the standard error codes, most methods will fail silently - and return false - * to access the error message either use $table->_lastError + * to access the error message either use $table->_lastError * or $last_error = PEAR::getStaticProperty('DB_DataObject','lastError'); * the code is $last_error->code, and the message is $last_error->message (a standard PEAR error) */ @@ -48,23 +48,23 @@ /** * The main "DB_DataObject" class is really a base class for your own tables classes - * + * * // Set up the class by creating an ini file (refer to the manual for more details * [DB_DataObject] * database = mysql:/username:password@host/database * schema_location = /home/myapplication/database * class_location = /home/myapplication/DBTables/ * clase_prefix = DBTables_ - * - * - * + * + * + * * //Start and initialize...................... - dont forget the & * $config = parse_ini_file('example.ini',TRUE); * $options = &PEAR::setStaticProperty('DB_DataObject','options'); * $options = $config['DB_DataObject']; - * + * * // example of a class (that does not use the 'auto generated tables data') - * class mytable extends DB_DataObject { + * class mytable extends DB_DataObject { * // mandatory - set the table * var $_database_dsn = "mysql://username:password@localhost/database"; * var $__table = "mytable"; @@ -78,19 +78,19 @@ * return array('id'); * } * } - * + * * // use in the application - * - * + * + * * Simple get one row - * + * * $instance = new mytable; * $instance->get("id",12); * echo $instance->somedata; - * - * + * + * * Get multiple rows - * + * * $instance = new mytable; * $instance->whereAdd("ID > 12"); * $instance->whereAdd("ID < 14"); @@ -98,8 +98,8 @@ * while ($instance->fetch()) { * echo $instance->somedata; * } - * - * + * + * * @package DB_DataObject * @author Alan Knowles <alan@akbkhome.com> * @since PHP 4.0 @@ -124,7 +124,7 @@ */ var $__table = ""; // database table - + /** * The Number of rows returned from a query * @@ -137,13 +137,13 @@ /* =========================================================== */ /* Major Public Methods (designed to be optionally then called with parent::method())*/ /* =========================================================== */ - + /** * get an result using key, value * - * for example + * for example * $object->get("ID",1234); * Returns Number of rows located (usually 1) for success, * and puts all the table columns into this classes variables @@ -159,8 +159,8 @@ * @access public * @return int No. of rows */ - - function get($k,$v=NULL) { + + function get($k,$v=NULL) { if ($v === NULL) { $v= $k; $keys = $this->_get_keys(); @@ -179,14 +179,14 @@ } $this->$k = $v; return $this->find(1); - } + } /** * a autoloading, caching static get method using key, value (based on get) * * Usage: - * $object = DB_DataObject::staticGet("DbTable_mytable",12); - * or + * $object = DB_DataObject::staticGet("DbTable_mytable",12); + * or * $object = DB_DataObject::staticGet("DbTable_mytable","name","fred"); * * or write it into your extended class: @@ -197,10 +197,10 @@ * @param string key value (optional) * @access public * @return object - */ - + */ + function &staticGet($class,$k,$v=NULL) { - + $cache = &PEAR::getStaticProperty('DB_DataObject','cache'); $key = "$k:$v"; @@ -216,7 +216,7 @@ if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { DB_DataObject::debug("$class $key","STATIC GET"); } - + $newclass = DB_DataObject::_autoloadClass($class); if (!$newclass) { DB_DataObject::raiseError("could not autoload $class", DB_DATAOBJECT_ERROR_NOCLASS); @@ -227,7 +227,7 @@ DB_DataObject::raiseError("Error creating $newclass", DB_DATAOBJECT_ERROR_NOCLASS); return; } - + if (!@$cache[$class]) { $cache[$class] = array(); } @@ -236,41 +236,41 @@ return; } $cache[$class][$key] = $obj; - return $cache[$class][$key]; + return $cache[$class][$key]; } /** * find results, either normal or crosstable * - * for example - * + * for example + * * $object = new mytable(); * $object->ID = 1; * $object->find(); - * + * * * will set $object->N to number of rows, and expects next command to fetch rows * will return $object->N * * @param boolean Fetch first result * @access public - * @return int + * @return int */ - - - function find($n=FALSE) { + + + function find($n=FALSE) { if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { - $this->debug($n, "__find",1); + $this->debug($n, "__find",1); } - if (!$this->__table) { + if (!$this->__table) { echo "NO \$__table SPECIFIED in class definition"; exit; } $this->N =0; $tmpcond = $this->_condition; $this->_build_condition($this->_get_table()) ; - $this->_query("SELECT ". + $this->_query("SELECT ". $this->_data_select . - " FROM ".$this->__table ." ". + " FROM ".$this->__table ." ". $this->_condition." ". $this->_group_by." ". $this->_order_by." ". @@ -278,11 +278,11 @@ ////add by ming ... keep old condition .. so that find can reuse $this->_condition = $tmpcond; if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { - $this->debug("CHECK autofetchd $n", "__find",1); + $this->debug("CHECK autofetchd $n", "__find",1); } if ($n && $this->N > 0 ) { if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { - $this->debug("ABOUT TO AUTOFETCH", "__find",1); + $this->debug("ABOUT TO AUTOFETCH", "__find",1); } $this->fetchRow(0) ; } @@ -292,13 +292,13 @@ return $this->N; } - + /** * fetches next row into this objects var's * * returns 1 on success 0 on failure * - * + * * * Example * $object = new mytable(); @@ -310,20 +310,20 @@ * $store[] = $object; // builds an array of object lines. * } * - * to add features to a fetch + * to add features to a fetch * function fetch () { * $ret = parent::fetch(); * $this->date_formated = date('dmY',$this->date); * return $ret; * } - * + * * @access public - * @return boolean on success + * @return boolean on success */ - function fetch() { + function fetch() { $results = &PEAR::getStaticProperty('DB_DataObject','results'); - + if (!@$this->N) { DB_DataObject::raiseError("fetch: No Data Availabe", DB_DATAOBJECT_ERROR_NODATA); return; @@ -333,13 +333,13 @@ if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug(serialize($array),"FETCH",3); } - + if (!is_array($array)) { // this is probably end of data!! //DB_DataObject::raiseError("fetch: no data returned", DB_DATAOBJECT_ERROR_NODATA); return; } - + foreach($array as $k=>$v) { $kk = str_replace(".", "_",$k); $kk = str_replace(" ", "_",$kk); @@ -347,19 +347,19 @@ $this->debug("$kk = ". $array[$k], "fetchrow LINE",3); } $this->$kk = $array[$k]; - } - + } + // set link flag $this->_link_loaded=FALSE; if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("{$this->__table} DONE", "fetchrow"); } - + return TRUE; } /** * Adds a condition to the WHERE statement, defaults to AND - * + * * $object->whereAdd(); //reset or cleaer ewhwer * $object->whereAdd("ID > 20"); * $object->whereAdd("age > 20","OR"); @@ -378,15 +378,15 @@ if ($this->_condition) { $this->_condition .= " {$logic} {$cond}"; return; - } + } $this->_condition = " WHERE {$cond}"; - + } - + /** - * Adds a order by condition - * + * Adds a order by condition + * * $object->orderBy(); //clears order by * $object->orderBy("ID"); * $object->orderBy("ID,age"); @@ -403,13 +403,13 @@ if (!$this->_order_by) { $this->_order_by = " ORDER BY {$order} "; return; - } + } $this->_order_by .= " , {$order}"; } - + /** - * Adds a group by condition - * + * Adds a group by condition + * * $object->groupBy(); //reset the grouping * $object->groupBy("ID DESC"); * $object->groupBy("ID,age"); @@ -426,13 +426,13 @@ if (!$this->_group_by) { $this->_group_by = " GROUP BY {$group} "; return; - } + } $this->_group_by .= " , {$group}"; } - + /** - * Sets the Limit - * + * Sets the Limit + * * $boject->limit(); // clear limit * $object->limit(12); * $object->limit(12,10); @@ -442,7 +442,7 @@ * @access public * @return none */ - + function limit($a=NULL,$b=NULL) { if ($a===NULL) { $this->_limit = ""; @@ -454,40 +454,40 @@ } $this->_limit = " LIMIT $a,$b "; } - - + + /** - * Adds a select columns - * + * Adds a select columns + * * $object->selectAdd(); // resets select to nothing! * $object->selectAdd("*"); // default select - * $object->selectAdd("unixtime(DATE) as udate"); + * $object->selectAdd("unixtime(DATE) as udate"); * $object->selectAdd("DATE"); * * * @access public * @return none */ - - + + function selectAdd($k=NULL) { if ($k===NULL) { $this->_data_select =''; return; } - if ($this->_data_select) + if ($this->_data_select) $this->_data_select .= ", "; $this->_data_select .= " $k "; } - - + + /** * Insert the current objects variables into the database * * Returns the ID of the inserted element - mysql specific = fixme? * - * for example - * + * for example + * * Designed to be extended * * $object = new mytable(); @@ -495,10 +495,10 @@ * echo $object->insert(); * * @access public - * @return int + * @return int */ - - function insert() { + + function insert() { $this->_connect(); $connections = &PEAR::getStaticProperty('DB_DataObject','connections'); $__DB= &$connections[$this->_database_dsn_md5]; @@ -507,7 +507,7 @@ DB_DataObject::raiseError("insert:No table definition for {$this->__table}", DB_DATAOBJECT_ERROR_INVALIDCONFIG); return; } - $datasaved=1; + $datasaved=1; $leftq=""; $rightq = ""; $key = FALSE; @@ -516,13 +516,13 @@ if (($key = @$keys[0]) && ($dbtype != 'mysql')) { $this->$key = $__DB->nextId(); } - + foreach($items as $k=>$v) { if (isset($this->$k)) { if ($leftq) { $leftq .= ", $k "; $rightq .=", '".addslashes($this->$k)."' "; - } else { + } else { $leftq = "$k "; $rightq .="'".addslashes($this->$k)."' "; } @@ -534,17 +534,17 @@ $this->$key = mysql_insert_id($connections[$this->_database_dsn_md5]->connection); $this->_clear_cache(); return $this->$key; - } + } DB_DataObject::raiseError("insert: No Data specifed for query", DB_DATAOBJECT_ERROR_NODATA); } /** * Updates current objects variables into the database * uses the _get_keys() to decide how to update - * Returns the TRUE on success + * Returns the TRUE on success + * + * for example * - * for example - * * $object = new mytable(); * $object->get("ID",234); * $object->email="testing@test.com"; @@ -552,18 +552,18 @@ * echo "UPDATE FAILED"; * * @access public - * @return boolean TRUE = success + * @return boolean TRUE = success */ - - function update() { + + function update() { $items = $this->_get_table(); $keys = $this->_get_keys(); - + if (!$items) { DB_DataObject::raiseError("update:No table definition for {$this->__table}", DB_DATAOBJECT_ERROR_INVALIDCONFIG); return; - } - $datasaved=1; + } + $datasaved=1; $settings =""; foreach($items as $k=>$v) { if (isset($this->$k) && !in_array($k, $keys)) { @@ -573,7 +573,7 @@ $settings .="$k = '".addslashes($this->$k)."' "; } } - + //$this->_condition=""; // dont clear condition if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("got keys as ".serialize($keys),3); @@ -581,19 +581,19 @@ $this->_build_condition($items,$keys); // echo " $settings, $this->condition "; if ($settings && $this->_condition) { - $this->_query("UPDATE {$this->__table} SET {$settings} {$this->_condition} "); + $this->_query("UPDATE {$this->__table} SET {$settings} {$this->_condition} "); $this->_clear_cache(); return TRUE; } DB_DataObject::raiseError("update: No Data specifed for query $settings , {$this->_condition}", DB_DATAOBJECT_ERROR_NODATA); } /** - * Deletes items from table which match current objects variables + * Deletes items from table which match current objects variables * * Returns the TRUE on success * - * for example - * + * for example + * * Designed to be extended * * $object = new mytable(); @@ -606,16 +606,16 @@ * @access public * @param boolean use the whereAdd conditions (default = no - use current values.) * @return boolean TRUE on success - */ - function delete($use_where=FALSE) { + */ + function delete($use_where=FALSE) { $keys = $this->_get_keys(); if (!$use_where) { $this->_condition=""; // default behaviour not to use where condition } - $this->_build_condition($this->_get_table()); - + $this->_build_condition($this->_get_table()); + if ($this->_condition) { - $this->_query("DELETE FROM {$this->__table} {$this->_condition}"); + $this->_query("DELETE FROM {$this->__table} {$this->_condition}"); $this->_clear_cache(); return TRUE; } @@ -627,17 +627,17 @@ * Not recommended - better to use fetch() * * Returens TRUE on success - * + * * @param int row * @access public * @return boolean TRUE on success */ - - - + + + function fetchRow($row=NULL) { if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { - $this->debug("{$this->__table} $row of {$this->N}", "fetchrow",3); + $this->debug("{$this->__table} $row of {$this->N}", "fetchrow",3); } if (!$this->__table) { DB_DataObject::raiseError("fetchrow: No table", DB_DATAOBJECT_ERROR_INVALIDCONFIG); @@ -651,51 +651,51 @@ DB_DataObject::raiseError("fetchrow: No results avaiable", DB_DATAOBJECT_ERROR_NODATA); return; } - if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { - $this->debug("{$this->__table} $row of {$this->N}", "fetchrow",3); + if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { + $this->debug("{$this->__table} $row of {$this->N}", "fetchrow",3); } $results = &PEAR::getStaticProperty('DB_DataObject','results'); - + $result = &$results[$this->_DB_resultid]; $array = $result->fetchrow(DB_FETCHMODE_ASSOC,$row); if (!is_array($array)) { DB_DataObject::raiseError("fetchrow: No results available", DB_DATAOBJECT_ERROR_NODATA); return; } - + foreach($array as $k=>$v) { $kk = str_replace(".", "_",$k); if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("$kk = ". $array[$k], "fetchrow LINE",3); } $this->$kk = $array[$k]; - } - + } + if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("{$this->__table} DONE", "fetchrow",3); } return TRUE; - - + + } - + /** * find the number of results from a simple query * - * for example - * + * for example + * * $object = new mytable(); * $object->name = "fred"; * echo $object->count(); * * @access public - * @return int + * @return int */ function count() { - + $items = $this->_get_table(); $tmpcond = $this->_condition; if ($items) { @@ -706,7 +706,7 @@ } } $keys = $this->_get_keys(); - + if (!$keys[0]) { echo "CAN NOT COUNT WITHOUT PRIMARY KEYS "; exit; @@ -719,7 +719,7 @@ $l=$result->fetchRow(DB_FETCHMODE_ASSOC,0); return $l["num"]; } - + /** * sends raw query to database * @@ -729,25 +729,25 @@ * @access public * @return none */ - - function query($string) { - $this->_query($string); - } - - - - - + + function query($string) { + $this->_query($string); + } + + + + + /* ==================================================== */ /* Major Private Vars */ /* ==================================================== */ - - + + /** * The Database connection dsn (as described in the PEAR DB) * only used really if you are writing a very simple application/test.. * try not to use this - it is better stored in configuration files.. - * + * * @access private * @var string */ @@ -758,78 +758,78 @@ * @var string */ var $_database_dsn_md5 = ""; - - - - - - + + + + + + /** * The Database name * created in __connection * @access private * @var string */ - var $_database = ""; + var $_database = ""; /** * The WHERE condition * @access private * @var string - */ + */ var $_condition=""; - + /** * The GROUP BY condition * @access private * @var string - */ + */ var $_group_by=""; /** * The ORDER BY condition * @access private * @var string - */ + */ var $_order_by=""; - + /** * The Limit by statement * @access private * @var string - */ + */ var $_limit=""; /** * The default Select * @access private * @var string - */ + */ var $_data_select="*"; - - + + /** * Database result id (references global $__DB_DataObject_results * @access private * @var integer - */ + */ var $_DB_resultid; // database result object - - - + + + /* =========================================================== */ /* Major Private Methods - the core part!*/ /* =========================================================== */ - + /** * Autoload the table definitions - * + * * * @param string database name * @param string table name * @access private * @return array */ - + function &_staticGetDefinitions($database,$table) { - + static $definitions = array(); if (@$definitions[$database][$table]) { return $definitions[$database][$table]; @@ -840,56 +840,56 @@ /* load the link table if it exists. */ if (file_exists($location."/{$database}.links.ini")) { $links = &PEAR::getStaticProperty('DB_DataObject', "{$database}.links"); - $linkConfig = parse_ini_file("config/{$db_name}.links.ini", true); + $linkConfig = parse_ini_file("config/{$database}.links.ini", true); $links = $linkConfig; } return $definitions[$database][$table]; - + } - - - + + + /** * get an associative array of table columns - * + * * @access private * @return array (associative) */ - function &_get_table() { + function &_get_table() { if (!@$this->_database) { $this->_connect(); } $ret = &DB_DataObject::_staticGetDefinitions($this->_database,$this->__table); return $ret; } - - - + + + /** - * get an array of table primary keys + * get an array of table primary keys * * This is defined in the table definition which should extend this class - * + * * @access private * @return array - */ - - - function &_get_keys() { + */ + + + function &_get_keys() { if (!@$this->_database) { $this->_connect(); } return array_keys(DB_DataObject::_staticGetDefinitions($this->_database,$this->__table."__keys")); } - - + + /** * clear the cache values for this class - normally done on insert/update etc. * * @access private * @return none */ - + function _clear_cache() { $cache = &PEAR::getStaticProperty('DB_DataObject','cache'); $class = get_class($this); @@ -897,23 +897,23 @@ unset($cache[$class]); } } - - + + /** * connects to the database * * @access private * @return none */ - + function _connect () { $connections = &PEAR::getStaticProperty('DB_DataObject','connections'); - - + + if ($this->_database_dsn_md5) {// already connected if (PEAR::isError($connections[$this->_database_dsn_md5])) { DB_DataObject::raiseError( - $connections[$this->_database_dsn_md5]->message, + $connections[$this->_database_dsn_md5]->message, $connections[$this->_database_dsn_md5]->code, PEAR_ERROR_DIE ); return; @@ -921,19 +921,19 @@ $this->_database = $connections[$this->_database_dsn_md5]->dsn["database"]; return; } - + $options= &PEAR::getStaticProperty('DB_DataObject','options'); $dsn = @$this->_database_dsn; - + if (!$dsn) { if ($database = @$options["table_{$this->__table}"]) { $dsn = $options["database_{$database}"]; - } else if ($options['database']) { + } else if ($options['database']) { $dsn = $options['database']; } } $this->_database_dsn_md5 = md5($dsn); - + if (@$connections[$this->_database_dsn_md5]) { if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("USING CACHE", "CONNECT",3); @@ -944,21 +944,21 @@ if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("NEW CONNECTION", "CONNECT",3); /* actualy make a connection */ - + $this->debug("{$dsn} {$this->_database_dsn_md5}", "CONNECT",3); } - $connections[$this->_database_dsn_md5] = DB::connect($dsn); + $connections[$this->_database_dsn_md5] = DB::connect($dsn); if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug(serialize($connections), "CONNECT",3); } if (PEAR::isError($connections[$this->_database_dsn_md5])) { DB_DataObject::raiseError( - $connections[$this->_database_dsn_md5]->message, + $connections[$this->_database_dsn_md5]->message, $connections[$this->_database_dsn_md5]->code, PEAR_ERROR_DIE ); - + } - + $this->_database = $connections[$this->_database_dsn_md5]->dsn["database"]; return TRUE; } @@ -968,22 +968,22 @@ * @access private * @return none */ - + function _query($string) { $connections = &PEAR::getStaticProperty('DB_DataObject','connections'); $results = &PEAR::getStaticProperty('DB_DataObject','results'); - - + + if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("QUERY".$string,$log="sql"); } $this->_connect(); $__DB = &$connections[$this->_database_dsn_md5]; - if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION'] && (DB_DataObject::debugLevel() > 1) && - (strtolower(substr(trim($string),0,6)) != "select") && + if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION'] && (DB_DataObject::debugLevel() > 1) && + (strtolower(substr(trim($string),0,6)) != "select") && (strtolower(substr(trim($string),0,4)) != "show") && (strtolower(substr(trim($string),0,8)) != "describe")) { - + $this->debug("Disabling Update as you are in debug mode"); return; } @@ -994,7 +994,7 @@ if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { DB_DataObject::debug($string, "SENT"); } - + DB_DataObject::raiseError($result->message, $result->code); return; } @@ -1004,7 +1004,7 @@ if (strtolower(substr(trim($string),0,6)) == "insert") { return; } - if (strtolower(substr(trim($string),0,6)) == "update") { + if (strtolower(substr(trim($string),0,6)) == "update") { return; } $this->N = 0; @@ -1014,23 +1014,23 @@ if (method_exists($result, "numrows")) { $this->N = $result->numrows(); } - + } - - - - - - - + + + + + + + /** * Builds the WHERE based on the values of of this object * * @access private * @return string - */ + */ function _build_condition(&$keys,$filter=array()) { - + foreach($keys as $k=>$v) { if ($filter) { if (!in_array($k,$filter)) { @@ -1040,7 +1040,7 @@ if (!isset($this->$k)) { continue; } - + if ($v & DB_DATAOBJECT_STR) { $this->whereAdd("{$k} = '".addslashes($this->$k) ."'"); continue; @@ -1060,25 +1060,25 @@ * @access private * @return string classname on Success */ - + function staticAutoloadTable($table) { $options= &PEAR::getStaticProperty('DB_DataObject','options'); $class = DB_DataObject::_autoloadClass($options['class_prefix'].ucfirst($table)); return $class; } - + /** - * autoload Class + * autoload Class * * @param Class * @access private * @return string classname on Success */ - + function _autoloadClass($class) { $options= &PEAR::getStaticProperty('DB_DataObject','options'); $table = substr($class,strlen($options['class_prefix'])); - + // this should/could autoload @include_once($options['require_prefix'].ucfirst($table).".php"); if (!class_exists($class)) { @@ -1092,9 +1092,9 @@ * Have the links been loaded? * @access private * @var boolean - */ - - var $_link_loaded=FALSE; + */ + + var $_link_loaded=FALSE; /** * load related objects * @@ -1106,7 +1106,7 @@ * looks up table xxxxx, for value id=$this->xxxxx * stores it in $this->_xxxxx_yyyyy * - * @author Time White <tlwhit2@qwest.com> + * @author Tim White <tim@cyface.com> * @access public * @return boolean , TRUE on success */ @@ -1144,7 +1144,7 @@ * looks up table xxxxx, for value id=$this->xxxxx * stores it in $this->_xxxxx_yyyyy * - * @author Time White <tlwhit2@qwest.com> + * @author Tim White <tim@cyface.com> * @access public * @return mixed object on success */ @@ -1171,7 +1171,7 @@ return; } - $class = $this->_autoloadTable($table); + $class = $this->staticAutoloadTable($table); if (!$class) { DB_DataObject::raiseError("getLink:Could not find class for row $row, table $table", DB_DATAOBJECT_ERROR_INVALIDCONFIG); return; @@ -1186,7 +1186,7 @@ * * This is highly dependant on naming columns 'correctly' :) * using colname = xxxxx_yyyyyy - * xxxxxx = related table; (yyyyy = user defined..) + * xxxxxx = related table; (yyyyy = user defined..) * looks up table xxxxx, for value id=$this->xxxxx * stores it in $this->_xxxxx_yyyyy * @@ -1220,18 +1220,18 @@ return $ret; } /** - * Copies items that are in the table definitions from an + * Copies items that are in the table definitions from an * array or object into the current object * will not override key values. - * * - * @param array | object + * + * @param array | object * @access public * @return boolean , TRUE on success */ - - - + + + function setFrom(&$from) { $keys = $this->_get_keys(); $items = $this->_get_table(); @@ -1247,7 +1247,7 @@ continue; // ignore empty keys!!! what } if (is_object($from) && @isset($from->$k)) { - $this->$k = $from->$k; + $this->$k = $from->$k; continue; } if (!@isset($from[$k])) { @@ -1262,25 +1262,25 @@ $this->$k = $from[$k]; } return TRUE; - }}} - + } + /** * validate - override this to set up your validation rules * * validate the current objects values either just testing strings/numbers or * using the user defined validate{Row name}() methods. * will attempt to call $this->validate{column_name}() - expects TRUE = ok FALSE = ERROR - * you can the use the validate Class from your own methods. - * + * you can the use the validate Class from your own methods. + * * @access public * @return array of validation results or TRUE */ - function validate() + function validate() { require_once("Validate.php"); $table = &$this->_get_table(); $ret = array(); - + foreach($table as $key=>$val) { if (!isset($this->$key)) continue; $method = "Validate".ucfirst($key); @@ -1292,23 +1292,23 @@ case DB_DATAOBJECT_STR: $ret[$key] = Validate::string($this->$key, VAL_PUNCTUATION . VAL_NAME); continue; - case DB_DATAOBJECT_INT: + case DB_DATAOBJECT_INT: $ret[$key] = Validate::number($this->$key, "."); continue; } } - + foreach ($ret as $key=>$val) { if ($val == FALSE) return $ret; } return TRUE; // everything is OK. } - - + + /* ----------------------- Debugger ------------------ */ /** * Debugger. - use this in your extended classes to output debugging information. - * + * * Uses DB_DataObject::DebugLevel(x) to turn it on * * @param string message - message to output @@ -1317,29 +1317,29 @@ * @access public * @return none */ - + function debug($message,$logtype=0, $level=1) { if ($GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { return; } - + if (DB_DataObject::debugLevel()<$level) { return; } - + if (!ini_get('html_errors')) { - echo "$logtype : $message\n"; + echo "$logtype : $message\n"; flush(); return; } - echo "<PRE><B>$logtype</B> $message</PRE>\n"; + echo "<PRE><B>$logtype</B> $message</PRE>\n"; flush(); } /** * sets and returns debug level * eg. DB_DataObject::debugLevel(4); * - * @param int level + * @param int level * @access public * @return none */ @@ -1353,24 +1353,24 @@ } return @$options['debug']; } - + /** * Last Error that has occured - * - use $this->_lastError or + * - use $this->_lastError or * $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError'); - * + * * @access public * @var object PEAR_Error (or FALSE) - */ - - var $_lastError=FALSE; - + */ + + var $_lastError=FALSE; + /** - * Default error handling is to create a pear error, but never return it. + * Default error handling is to create a pear error, but never return it. * if you need to handle errors you should look at setting the PEAR_Error callback * this is due to the fact it would wreck havoc on the internal methods! * - * @param int message + * @param int message * @param int type * @param int behaviour (die or contine!); * @access public @@ -1380,10 +1380,10 @@ $error = PEAR::raiseError($message,$type,$behaviour); if (is_object($this)) { $this->_lastError = $error; - } + } $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError'); $last_error = $error; - + DB_DataObject::debug($message,"ERROR",1); } @@ -1416,5 +1416,5 @@ - + ?>
Index: pear/DB_DataObject/DataObject.php diff -u pear/DB_DataObject/DataObject.php:1.20 pear/DB_DataObject/DataObject.php:1.21 --- pear/DB_DataObject/DataObject.php:1.20 Thu Aug 15 01:13:51 2002 +++ pear/DB_DataObject/DataObject.php Fri Aug 16 01:15:53 2002 @@ -15,7 +15,7 @@ // | Author: Alan Knowles <alan@akbkhome.com> // +----------------------------------------------------------------------+ // -// $Id: DataObject.php,v 1.20 2002/08/15 05:13:51 alan_k Exp $ +// $Id: DataObject.php,v 1.21 2002/08/16 05:15:53 alan_k Exp $ // // Object Based Database Query Builder and data store // @@ -23,7 +23,7 @@ require_once 'PEAR.php'; /** - * these are constants for the get_table array + * these are constants for the get_table array * user to determine what type of escaping is required around the object vars. */ define("DB_DATAOBJECT_INT", 1); // does not require '' @@ -33,9 +33,9 @@ define("DB_DATAOBJECT_BOOL", 16); // is boolean #TODO define("DB_DATAOBJECT_TXT", 32); // is long text #TODO -/* +/* * Theses are the standard error codes, most methods will fail silently - and return false - * to access the error message either use $table->_lastError + * to access the error message either use $table->_lastError * or $last_error = PEAR::getStaticProperty('DB_DataObject','lastError'); * the code is $last_error->code, and the message is $last_error->message (a standard PEAR error) */ @@ -48,23 +48,23 @@ /** * The main "DB_DataObject" class is really a base class for your own tables classes - * + * * // Set up the class by creating an ini file (refer to the manual for more details * [DB_DataObject] * database = mysql:/username:password@host/database * schema_location = /home/myapplication/database * class_location = /home/myapplication/DBTables/ * clase_prefix = DBTables_ - * - * - * + * + * + * * //Start and initialize...................... - dont forget the & * $config = parse_ini_file('example.ini',TRUE); * $options = &PEAR::setStaticProperty('DB_DataObject','options'); * $options = $config['DB_DataObject']; - * + * * // example of a class (that does not use the 'auto generated tables data') - * class mytable extends DB_DataObject { + * class mytable extends DB_DataObject { * // mandatory - set the table * var $_database_dsn = "mysql://username:password@localhost/database"; * var $__table = "mytable"; @@ -78,19 +78,19 @@ * return array('id'); * } * } - * + * * // use in the application - * - * + * + * * Simple get one row - * + * * $instance = new mytable; * $instance->get("id",12); * echo $instance->somedata; - * - * + * + * * Get multiple rows - * + * * $instance = new mytable; * $instance->whereAdd("ID > 12"); * $instance->whereAdd("ID < 14"); @@ -98,8 +98,8 @@ * while ($instance->fetch()) { * echo $instance->somedata; * } - * - * + * + * * @package DB_DataObject * @author Alan Knowles <alan@akbkhome.com> * @since PHP 4.0 @@ -124,7 +124,7 @@ */ var $__table = ""; // database table - + /** * The Number of rows returned from a query * @@ -137,13 +137,13 @@ /* =========================================================== */ /* Major Public Methods (designed to be optionally then called with parent::method())*/ /* =========================================================== */ - + /** * get an result using key, value * - * for example + * for example * $object->get("ID",1234); * Returns Number of rows located (usually 1) for success, * and puts all the table columns into this classes variables @@ -159,8 +159,8 @@ * @access public * @return int No. of rows */ - - function get($k,$v=NULL) { + + function get($k,$v=NULL) { if ($v === NULL) { $v= $k; $keys = $this->_get_keys(); @@ -179,14 +179,14 @@ } $this->$k = $v; return $this->find(1); - } + } /** * a autoloading, caching static get method using key, value (based on get) * * Usage: - * $object = DB_DataObject::staticGet("DbTable_mytable",12); - * or + * $object = DB_DataObject::staticGet("DbTable_mytable",12); + * or * $object = DB_DataObject::staticGet("DbTable_mytable","name","fred"); * * or write it into your extended class: @@ -197,10 +197,10 @@ * @param string key value (optional) * @access public * @return object - */ - + */ + function &staticGet($class,$k,$v=NULL) { - + $cache = &PEAR::getStaticProperty('DB_DataObject','cache'); $key = "$k:$v"; @@ -216,7 +216,7 @@ if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { DB_DataObject::debug("$class $key","STATIC GET"); } - + $newclass = DB_DataObject::_autoloadClass($class); if (!$newclass) { DB_DataObject::raiseError("could not autoload $class", DB_DATAOBJECT_ERROR_NOCLASS); @@ -227,7 +227,7 @@ DB_DataObject::raiseError("Error creating $newclass", DB_DATAOBJECT_ERROR_NOCLASS); return; } - + if (!@$cache[$class]) { $cache[$class] = array(); } @@ -236,41 +236,41 @@ return; } $cache[$class][$key] = $obj; - return $cache[$class][$key]; + return $cache[$class][$key]; } /** * find results, either normal or crosstable * - * for example - * + * for example + * * $object = new mytable(); * $object->ID = 1; * $object->find(); - * + * * * will set $object->N to number of rows, and expects next command to fetch rows * will return $object->N * * @param boolean Fetch first result * @access public - * @return int + * @return int */ - - - function find($n=FALSE) { + + + function find($n=FALSE) { if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { - $this->debug($n, "__find",1); + $this->debug($n, "__find",1); } - if (!$this->__table) { + if (!$this->__table) { echo "NO \$__table SPECIFIED in class definition"; exit; } $this->N =0; $tmpcond = $this->_condition; $this->_build_condition($this->_get_table()) ; - $this->_query("SELECT ". + $this->_query("SELECT ". $this->_data_select . - " FROM ".$this->__table ." ". + " FROM ".$this->__table ." ". $this->_condition." ". $this->_group_by." ". $this->_order_by." ". @@ -278,11 +278,11 @@ ////add by ming ... keep old condition .. so that find can reuse $this->_condition = $tmpcond; if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { - $this->debug("CHECK autofetchd $n", "__find",1); + $this->debug("CHECK autofetchd $n", "__find",1); } if ($n && $this->N > 0 ) { if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { - $this->debug("ABOUT TO AUTOFETCH", "__find",1); + $this->debug("ABOUT TO AUTOFETCH", "__find",1); } $this->fetchRow(0) ; } @@ -292,13 +292,13 @@ return $this->N; } - + /** * fetches next row into this objects var's * * returns 1 on success 0 on failure * - * + * * * Example * $object = new mytable(); @@ -310,20 +310,20 @@ * $store[] = $object; // builds an array of object lines. * } * - * to add features to a fetch + * to add features to a fetch * function fetch () { * $ret = parent::fetch(); * $this->date_formated = date('dmY',$this->date); * return $ret; * } - * + * * @access public - * @return boolean on success + * @return boolean on success */ - function fetch() { + function fetch() { $results = &PEAR::getStaticProperty('DB_DataObject','results'); - + if (!@$this->N) { DB_DataObject::raiseError("fetch: No Data Availabe", DB_DATAOBJECT_ERROR_NODATA); return; @@ -333,13 +333,13 @@ if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug(serialize($array),"FETCH",3); } - + if (!is_array($array)) { // this is probably end of data!! //DB_DataObject::raiseError("fetch: no data returned", DB_DATAOBJECT_ERROR_NODATA); return; } - + foreach($array as $k=>$v) { $kk = str_replace(".", "_",$k); $kk = str_replace(" ", "_",$kk); @@ -347,19 +347,19 @@ $this->debug("$kk = ". $array[$k], "fetchrow LINE",3); } $this->$kk = $array[$k]; - } - + } + // set link flag $this->_link_loaded=FALSE; if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("{$this->__table} DONE", "fetchrow"); } - + return TRUE; } /** * Adds a condition to the WHERE statement, defaults to AND - * + * * $object->whereAdd(); //reset or cleaer ewhwer * $object->whereAdd("ID > 20"); * $object->whereAdd("age > 20","OR"); @@ -378,15 +378,15 @@ if ($this->_condition) { $this->_condition .= " {$logic} {$cond}"; return; - } + } $this->_condition = " WHERE {$cond}"; - + } - + /** - * Adds a order by condition - * + * Adds a order by condition + * * $object->orderBy(); //clears order by * $object->orderBy("ID"); * $object->orderBy("ID,age"); @@ -403,13 +403,13 @@ if (!$this->_order_by) { $this->_order_by = " ORDER BY {$order} "; return; - } + } $this->_order_by .= " , {$order}"; } - + /** - * Adds a group by condition - * + * Adds a group by condition + * * $object->groupBy(); //reset the grouping * $object->groupBy("ID DESC"); * $object->groupBy("ID,age"); @@ -426,13 +426,13 @@ if (!$this->_group_by) { $this->_group_by = " GROUP BY {$group} "; return; - } + } $this->_group_by .= " , {$group}"; } - + /** - * Sets the Limit - * + * Sets the Limit + * * $boject->limit(); // clear limit * $object->limit(12); * $object->limit(12,10); @@ -442,7 +442,7 @@ * @access public * @return none */ - + function limit($a=NULL,$b=NULL) { if ($a===NULL) { $this->_limit = ""; @@ -454,40 +454,40 @@ } $this->_limit = " LIMIT $a,$b "; } - - + + /** - * Adds a select columns - * + * Adds a select columns + * * $object->selectAdd(); // resets select to nothing! * $object->selectAdd("*"); // default select - * $object->selectAdd("unixtime(DATE) as udate"); + * $object->selectAdd("unixtime(DATE) as udate"); * $object->selectAdd("DATE"); * * * @access public * @return none */ - - + + function selectAdd($k=NULL) { if ($k===NULL) { $this->_data_select =''; return; } - if ($this->_data_select) + if ($this->_data_select) $this->_data_select .= ", "; $this->_data_select .= " $k "; } - - + + /** * Insert the current objects variables into the database * * Returns the ID of the inserted element - mysql specific = fixme? * - * for example - * + * for example + * * Designed to be extended * * $object = new mytable(); @@ -495,10 +495,10 @@ * echo $object->insert(); * * @access public - * @return int + * @return int */ - - function insert() { + + function insert() { $this->_connect(); $connections = &PEAR::getStaticProperty('DB_DataObject','connections'); $__DB= &$connections[$this->_database_dsn_md5]; @@ -507,7 +507,7 @@ DB_DataObject::raiseError("insert:No table definition for {$this->__table}", DB_DATAOBJECT_ERROR_INVALIDCONFIG); return; } - $datasaved=1; + $datasaved=1; $leftq=""; $rightq = ""; $key = FALSE; @@ -516,13 +516,13 @@ if (($key = @$keys[0]) && ($dbtype != 'mysql')) { $this->$key = $__DB->nextId(); } - + foreach($items as $k=>$v) { if (isset($this->$k)) { if ($leftq) { $leftq .= ", $k "; $rightq .=", '".addslashes($this->$k)."' "; - } else { + } else { $leftq = "$k "; $rightq .="'".addslashes($this->$k)."' "; } @@ -534,17 +534,17 @@ $this->$key = mysql_insert_id($connections[$this->_database_dsn_md5]->connection); $this->_clear_cache(); return $this->$key; - } + } DB_DataObject::raiseError("insert: No Data specifed for query", DB_DATAOBJECT_ERROR_NODATA); } /** * Updates current objects variables into the database * uses the _get_keys() to decide how to update - * Returns the TRUE on success + * Returns the TRUE on success + * + * for example * - * for example - * * $object = new mytable(); * $object->get("ID",234); * $object->email="testing@test.com"; @@ -552,18 +552,18 @@ * echo "UPDATE FAILED"; * * @access public - * @return boolean TRUE = success + * @return boolean TRUE = success */ - - function update() { + + function update() { $items = $this->_get_table(); $keys = $this->_get_keys(); - + if (!$items) { DB_DataObject::raiseError("update:No table definition for {$this->__table}", DB_DATAOBJECT_ERROR_INVALIDCONFIG); return; - } - $datasaved=1; + } + $datasaved=1; $settings =""; foreach($items as $k=>$v) { if (isset($this->$k) && !in_array($k, $keys)) { @@ -573,7 +573,7 @@ $settings .="$k = '".addslashes($this->$k)."' "; } } - + //$this->_condition=""; // dont clear condition if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("got keys as ".serialize($keys),3); @@ -581,19 +581,19 @@ $this->_build_condition($items,$keys); // echo " $settings, $this->condition "; if ($settings && $this->_condition) { - $this->_query("UPDATE {$this->__table} SET {$settings} {$this->_condition} "); + $this->_query("UPDATE {$this->__table} SET {$settings} {$this->_condition} "); $this->_clear_cache(); return TRUE; } DB_DataObject::raiseError("update: No Data specifed for query $settings , {$this->_condition}", DB_DATAOBJECT_ERROR_NODATA); } /** - * Deletes items from table which match current objects variables + * Deletes items from table which match current objects variables * * Returns the TRUE on success * - * for example - * + * for example + * * Designed to be extended * * $object = new mytable(); @@ -606,16 +606,16 @@ * @access public * @param boolean use the whereAdd conditions (default = no - use current values.) * @return boolean TRUE on success - */ - function delete($use_where=FALSE) { + */ + function delete($use_where=FALSE) { $keys = $this->_get_keys(); if (!$use_where) { $this->_condition=""; // default behaviour not to use where condition } - $this->_build_condition($this->_get_table()); - + $this->_build_condition($this->_get_table()); + if ($this->_condition) { - $this->_query("DELETE FROM {$this->__table} {$this->_condition}"); + $this->_query("DELETE FROM {$this->__table} {$this->_condition}"); $this->_clear_cache(); return TRUE; } @@ -627,17 +627,17 @@ * Not recommended - better to use fetch() * * Returens TRUE on success - * + * * @param int row * @access public * @return boolean TRUE on success */ - - - + + + function fetchRow($row=NULL) { if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { - $this->debug("{$this->__table} $row of {$this->N}", "fetchrow",3); + $this->debug("{$this->__table} $row of {$this->N}", "fetchrow",3); } if (!$this->__table) { DB_DataObject::raiseError("fetchrow: No table", DB_DATAOBJECT_ERROR_INVALIDCONFIG); @@ -651,51 +651,51 @@ DB_DataObject::raiseError("fetchrow: No results avaiable", DB_DATAOBJECT_ERROR_NODATA); return; } - if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { - $this->debug("{$this->__table} $row of {$this->N}", "fetchrow",3); + if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { + $this->debug("{$this->__table} $row of {$this->N}", "fetchrow",3); } $results = &PEAR::getStaticProperty('DB_DataObject','results'); - + $result = &$results[$this->_DB_resultid]; $array = $result->fetchrow(DB_FETCHMODE_ASSOC,$row); if (!is_array($array)) { DB_DataObject::raiseError("fetchrow: No results available", DB_DATAOBJECT_ERROR_NODATA); return; } - + foreach($array as $k=>$v) { $kk = str_replace(".", "_",$k); if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("$kk = ". $array[$k], "fetchrow LINE",3); } $this->$kk = $array[$k]; - } - + } + if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("{$this->__table} DONE", "fetchrow",3); } return TRUE; - - + + } - + /** * find the number of results from a simple query * - * for example - * + * for example + * * $object = new mytable(); * $object->name = "fred"; * echo $object->count(); * * @access public - * @return int + * @return int */ function count() { - + $items = $this->_get_table(); $tmpcond = $this->_condition; if ($items) { @@ -706,7 +706,7 @@ } } $keys = $this->_get_keys(); - + if (!$keys[0]) { echo "CAN NOT COUNT WITHOUT PRIMARY KEYS "; exit; @@ -719,7 +719,7 @@ $l=$result->fetchRow(DB_FETCHMODE_ASSOC,0); return $l["num"]; } - + /** * sends raw query to database * @@ -729,25 +729,25 @@ * @access public * @return none */ - - function query($string) { - $this->_query($string); - } - - - - - + + function query($string) { + $this->_query($string); + } + + + + + /* ==================================================== */ /* Major Private Vars */ /* ==================================================== */ - - + + /** * The Database connection dsn (as described in the PEAR DB) * only used really if you are writing a very simple application/test.. * try not to use this - it is better stored in configuration files.. - * + * * @access private * @var string */ @@ -758,78 +758,78 @@ * @var string */ var $_database_dsn_md5 = ""; - - - - - - + + + + + + /** * The Database name * created in __connection * @access private * @var string */ - var $_database = ""; + var $_database = ""; /** * The WHERE condition * @access private * @var string - */ + */ var $_condition=""; - + /** * The GROUP BY condition * @access private * @var string - */ + */ var $_group_by=""; /** * The ORDER BY condition * @access private * @var string - */ + */ var $_order_by=""; - + /** * The Limit by statement * @access private * @var string - */ + */ var $_limit=""; /** * The default Select * @access private * @var string - */ + */ var $_data_select="*"; - - + + /** * Database result id (references global $__DB_DataObject_results * @access private * @var integer - */ + */ var $_DB_resultid; // database result object - - - + + + /* =========================================================== */ /* Major Private Methods - the core part!*/ /* =========================================================== */ - + /** * Autoload the table definitions - * + * * * @param string database name * @param string table name * @access private * @return array */ - + function &_staticGetDefinitions($database,$table) { - + static $definitions = array(); if (@$definitions[$database][$table]) { return $definitions[$database][$table]; @@ -840,56 +840,56 @@ /* load the link table if it exists. */ if (file_exists($location."/{$database}.links.ini")) { $links = &PEAR::getStaticProperty('DB_DataObject', "{$database}.links"); - $linkConfig = parse_ini_file("config/{$db_name}.links.ini", true); + $linkConfig = parse_ini_file("config/{$database}.links.ini", true); $links = $linkConfig; } return $definitions[$database][$table]; - + } - - - + + + /** * get an associative array of table columns - * + * * @access private * @return array (associative) */ - function &_get_table() { + function &_get_table() { if (!@$this->_database) { $this->_connect(); } $ret = &DB_DataObject::_staticGetDefinitions($this->_database,$this->__table); return $ret; } - - - + + + /** - * get an array of table primary keys + * get an array of table primary keys * * This is defined in the table definition which should extend this class - * + * * @access private * @return array - */ - - - function &_get_keys() { + */ + + + function &_get_keys() { if (!@$this->_database) { $this->_connect(); } return array_keys(DB_DataObject::_staticGetDefinitions($this->_database,$this->__table."__keys")); } - - + + /** * clear the cache values for this class - normally done on insert/update etc. * * @access private * @return none */ - + function _clear_cache() { $cache = &PEAR::getStaticProperty('DB_DataObject','cache'); $class = get_class($this); @@ -897,23 +897,23 @@ unset($cache[$class]); } } - - + + /** * connects to the database * * @access private * @return none */ - + function _connect () { $connections = &PEAR::getStaticProperty('DB_DataObject','connections'); - - + + if ($this->_database_dsn_md5) {// already connected if (PEAR::isError($connections[$this->_database_dsn_md5])) { DB_DataObject::raiseError( - $connections[$this->_database_dsn_md5]->message, + $connections[$this->_database_dsn_md5]->message, $connections[$this->_database_dsn_md5]->code, PEAR_ERROR_DIE ); return; @@ -921,19 +921,19 @@ $this->_database = $connections[$this->_database_dsn_md5]->dsn["database"]; return; } - + $options= &PEAR::getStaticProperty('DB_DataObject','options'); $dsn = @$this->_database_dsn; - + if (!$dsn) { if ($database = @$options["table_{$this->__table}"]) { $dsn = $options["database_{$database}"]; - } else if ($options['database']) { + } else if ($options['database']) { $dsn = $options['database']; } } $this->_database_dsn_md5 = md5($dsn); - + if (@$connections[$this->_database_dsn_md5]) { if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("USING CACHE", "CONNECT",3); @@ -944,21 +944,21 @@ if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("NEW CONNECTION", "CONNECT",3); /* actualy make a connection */ - + $this->debug("{$dsn} {$this->_database_dsn_md5}", "CONNECT",3); } - $connections[$this->_database_dsn_md5] = DB::connect($dsn); + $connections[$this->_database_dsn_md5] = DB::connect($dsn); if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug(serialize($connections), "CONNECT",3); } if (PEAR::isError($connections[$this->_database_dsn_md5])) { DB_DataObject::raiseError( - $connections[$this->_database_dsn_md5]->message, + $connections[$this->_database_dsn_md5]->message, $connections[$this->_database_dsn_md5]->code, PEAR_ERROR_DIE ); - + } - + $this->_database = $connections[$this->_database_dsn_md5]->dsn["database"]; return TRUE; } @@ -968,22 +968,22 @@ * @access private * @return none */ - + function _query($string) { $connections = &PEAR::getStaticProperty('DB_DataObject','connections'); $results = &PEAR::getStaticProperty('DB_DataObject','results'); - - + + if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { $this->debug("QUERY".$string,$log="sql"); } $this->_connect(); $__DB = &$connections[$this->_database_dsn_md5]; - if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION'] && (DB_DataObject::debugLevel() > 1) && - (strtolower(substr(trim($string),0,6)) != "select") && + if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION'] && (DB_DataObject::debugLevel() > 1) && + (strtolower(substr(trim($string),0,6)) != "select") && (strtolower(substr(trim($string),0,4)) != "show") && (strtolower(substr(trim($string),0,8)) != "describe")) { - + $this->debug("Disabling Update as you are in debug mode"); return; } @@ -994,7 +994,7 @@ if (!$GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { DB_DataObject::debug($string, "SENT"); } - + DB_DataObject::raiseError($result->message, $result->code); return; } @@ -1004,7 +1004,7 @@ if (strtolower(substr(trim($string),0,6)) == "insert") { return; } - if (strtolower(substr(trim($string),0,6)) == "update") { + if (strtolower(substr(trim($string),0,6)) == "update") { return; } $this->N = 0; @@ -1014,23 +1014,23 @@ if (method_exists($result, "numrows")) { $this->N = $result->numrows(); } - + } - - - - - - - + + + + + + + /** * Builds the WHERE based on the values of of this object * * @access private * @return string - */ + */ function _build_condition(&$keys,$filter=array()) { - + foreach($keys as $k=>$v) { if ($filter) { if (!in_array($k,$filter)) { @@ -1040,7 +1040,7 @@ if (!isset($this->$k)) { continue; } - + if ($v & DB_DATAOBJECT_STR) { $this->whereAdd("{$k} = '".addslashes($this->$k) ."'"); continue; @@ -1060,25 +1060,25 @@ * @access private * @return string classname on Success */ - + function staticAutoloadTable($table) { $options= &PEAR::getStaticProperty('DB_DataObject','options'); $class = DB_DataObject::_autoloadClass($options['class_prefix'].ucfirst($table)); return $class; } - + /** - * autoload Class + * autoload Class * * @param Class * @access private * @return string classname on Success */ - + function _autoloadClass($class) { $options= &PEAR::getStaticProperty('DB_DataObject','options'); $table = substr($class,strlen($options['class_prefix'])); - + // this should/could autoload @include_once($options['require_prefix'].ucfirst($table).".php"); if (!class_exists($class)) { @@ -1092,9 +1092,9 @@ * Have the links been loaded? * @access private * @var boolean - */ - - var $_link_loaded=FALSE; + */ + + var $_link_loaded=FALSE; /** * load related objects * @@ -1106,7 +1106,7 @@ * looks up table xxxxx, for value id=$this->xxxxx * stores it in $this->_xxxxx_yyyyy * - * @author Time White <tlwhit2@qwest.com> + * @author Tim White <tim@cyface.com> * @access public * @return boolean , TRUE on success */ @@ -1144,7 +1144,7 @@ * looks up table xxxxx, for value id=$this->xxxxx * stores it in $this->_xxxxx_yyyyy * - * @author Time White <tlwhit2@qwest.com> + * @author Tim White <tim@cyface.com> * @access public * @return mixed object on success */ @@ -1171,7 +1171,7 @@ return; } - $class = $this->_autoloadTable($table); + $class = $this->staticAutoloadTable($table); if (!$class) { DB_DataObject::raiseError("getLink:Could not find class for row $row, table $table", DB_DATAOBJECT_ERROR_INVALIDCONFIG); return; @@ -1186,7 +1186,7 @@ * * This is highly dependant on naming columns 'correctly' :) * using colname = xxxxx_yyyyyy - * xxxxxx = related table; (yyyyy = user defined..) + * xxxxxx = related table; (yyyyy = user defined..) * looks up table xxxxx, for value id=$this->xxxxx * stores it in $this->_xxxxx_yyyyy * @@ -1220,18 +1220,18 @@ return $ret; } /** - * Copies items that are in the table definitions from an + * Copies items that are in the table definitions from an * array or object into the current object * will not override key values. - * * - * @param array | object + * + * @param array | object * @access public * @return boolean , TRUE on success */ - - - + + + function setFrom(&$from) { $keys = $this->_get_keys(); $items = $this->_get_table(); @@ -1247,7 +1247,7 @@ continue; // ignore empty keys!!! what } if (is_object($from) && @isset($from->$k)) { - $this->$k = $from->$k; + $this->$k = $from->$k; continue; } if (!@isset($from[$k])) { @@ -1262,25 +1262,25 @@ $this->$k = $from[$k]; } return TRUE; - }}} - + } + /** * validate - override this to set up your validation rules * * validate the current objects values either just testing strings/numbers or * using the user defined validate{Row name}() methods. * will attempt to call $this->validate{column_name}() - expects TRUE = ok FALSE = ERROR - * you can the use the validate Class from your own methods. - * + * you can the use the validate Class from your own methods. + * * @access public * @return array of validation results or TRUE */ - function validate() + function validate() { require_once("Validate.php"); $table = &$this->_get_table(); $ret = array(); - + foreach($table as $key=>$val) { if (!isset($this->$key)) continue; $method = "Validate".ucfirst($key); @@ -1292,23 +1292,23 @@ case DB_DATAOBJECT_STR: $ret[$key] = Validate::string($this->$key, VAL_PUNCTUATION . VAL_NAME); continue; - case DB_DATAOBJECT_INT: + case DB_DATAOBJECT_INT: $ret[$key] = Validate::number($this->$key, "."); continue; } } - + foreach ($ret as $key=>$val) { if ($val == FALSE) return $ret; } return TRUE; // everything is OK. } - - + + /* ----------------------- Debugger ------------------ */ /** * Debugger. - use this in your extended classes to output debugging information. - * + * * Uses DB_DataObject::DebugLevel(x) to turn it on * * @param string message - message to output @@ -1317,29 +1317,29 @@ * @access public * @return none */ - + function debug($message,$logtype=0, $level=1) { if ($GLOBALS['_DB_DATAOBJECT_PRODUCTION']) { return; } - + if (DB_DataObject::debugLevel()<$level) { return; } - + if (!ini_get('html_errors')) { - echo "$logtype : $message\n"; + echo "$logtype : $message\n"; flush(); return; } - echo "<PRE><B>$logtype</B> $message</PRE>\n"; + echo "<PRE><B>$logtype</B> $message</PRE>\n"; flush(); } /** * sets and returns debug level * eg. DB_DataObject::debugLevel(4); * - * @param int level + * @param int level * @access public * @return none */ @@ -1353,24 +1353,24 @@ } return @$options['debug']; } - + /** * Last Error that has occured - * - use $this->_lastError or + * - use $this->_lastError or * $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError'); - * + * * @access public * @var object PEAR_Error (or FALSE) - */ - - var $_lastError=FALSE; - + */ + + var $_lastError=FALSE; + /** - * Default error handling is to create a pear error, but never return it. + * Default error handling is to create a pear error, but never return it. * if you need to handle errors you should look at setting the PEAR_Error callback * this is due to the fact it would wreck havoc on the internal methods! * - * @param int message + * @param int message * @param int type * @param int behaviour (die or contine!); * @access public @@ -1380,10 +1380,10 @@ $error = PEAR::raiseError($message,$type,$behaviour); if (is_object($this)) { $this->_lastError = $error; - } + } $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError'); $last_error = $error; - + DB_DataObject::debug($message,"ERROR",1); } @@ -1416,5 +1416,5 @@ - + ?>