diff options
Diffstat (limited to 'adodb-active-recordx.inc.php')
| -rw-r--r-- | adodb-active-recordx.inc.php | 541 |
1 files changed, 262 insertions, 279 deletions
diff --git a/adodb-active-recordx.inc.php b/adodb-active-recordx.inc.php index cee95c66..901e3b10 100644 --- a/adodb-active-recordx.inc.php +++ b/adodb-active-recordx.inc.php @@ -37,12 +37,14 @@ $_ADODB_ACTIVE_DBS = array(); $ACTIVE_RECORD_SAFETY = true; // CFR: disabled while playing with relations $ADODB_ACTIVE_DEFVALS = false; -class ADODB_Active_DB { +class ADODB_Active_DB +{ var $db; // ADOConnection var $tables; // assoc array of ADODB_Active_Table objects, indexed by tablename } -class ADODB_Active_Table { +class ADODB_Active_Table +{ var $name; // table name var $flds; // assoc array of adofieldobjs, indexed by fieldname var $keys; // assoc array of primary keys, indexed by fieldname @@ -54,9 +56,9 @@ class ADODB_Active_Table { function updateColsCount() { $this->_colsCount = sizeof($this->flds); - foreach($this->_belongsTo as $foreignTable) + foreach ($this->_belongsTo as $foreignTable) $this->_colsCount += sizeof($foreignTable->TableInfo()->flds); - foreach($this->_hasMany as $foreignTable) + foreach ($this->_hasMany as $foreignTable) $this->_colsCount += sizeof($foreignTable->TableInfo()->flds); } } @@ -66,7 +68,7 @@ function ADODB_SetDatabaseAdapter(&$db) { global $_ADODB_ACTIVE_DBS; - foreach($_ADODB_ACTIVE_DBS as $k => $d) { + foreach ($_ADODB_ACTIVE_DBS as $k => $d) { if ($d->db === $db) { return $k; } @@ -78,11 +80,12 @@ function ADODB_SetDatabaseAdapter(&$db) $_ADODB_ACTIVE_DBS[] = $obj; - return sizeof($_ADODB_ACTIVE_DBS)-1; + return sizeof($_ADODB_ACTIVE_DBS) - 1; } -class ADODB_Active_Record { +class ADODB_Active_Record +{ static $_changeNames = true; // dynamically pluralize table names static $_foreignSuffix = '_id'; // var $_dbat; // associative index pointing to ADODB_Active_DB eg. $ADODB_Active_DBS[_dbat] @@ -97,9 +100,9 @@ class ADODB_Active_Record { var $foreignName; // CFR: class name when in a relationship - static function UseDefaultValues($bool=null) + static function UseDefaultValues($bool = null) { - global $ADODB_ACTIVE_DEFVALS; + global $ADODB_ACTIVE_DEFVALS; if (isset($bool)) { $ADODB_ACTIVE_DEFVALS = $bool; } @@ -129,22 +132,21 @@ class ADODB_Active_Record { // $options is an array that allows us to tweak the constructor's behaviour // if $options['refresh'] is true, we re-scan our metadata information // if $options['new'] is true, we forget all relations - function __construct($table = false, $pkeyarr=false, $db=false, $options=array()) + function __construct($table = false, $pkeyarr = false, $db = false, $options = array()) { - global $_ADODB_ACTIVE_DBS; + global $_ADODB_ACTIVE_DBS; if ($db == false && is_object($pkeyarr)) { $db = $pkeyarr; $pkeyarr = false; } - if($table) { + if ($table) { // table argument exists. It is expected to be // already plural form. $this->_pTable = $table; $this->_sTable = $this->_singularize($this->_pTable); - } - else { + } else { // We will use current classname as table name. // We need to pluralize it for the real table name. $this->_sTable = strtolower(get_class($this)); @@ -157,7 +159,7 @@ class ADODB_Active_Record { if ($db) { $this->_dbat = ADODB_Active_Record::SetDatabaseAdapter($db); } else - $this->_dbat = sizeof($_ADODB_ACTIVE_DBS)-1; + $this->_dbat = sizeof($_ADODB_ACTIVE_DBS) - 1; if ($this->_dbat < 0) { @@ -173,7 +175,7 @@ class ADODB_Active_Record { // but there was no way to ask it to do that. $forceUpdate = (isset($options['refresh']) && true === $options['refresh']); $this->UpdateActiveTable($pkeyarr, $forceUpdate); - if(isset($options['new']) && true === $options['new']) { + if (isset($options['new']) && true === $options['new']) { $table =& $this->TableInfo(); unset($table->_hasMany); unset($table->_belongsTo); @@ -190,30 +192,30 @@ class ADODB_Active_Record { // CFR: Constants found in Rails static $IrregularP = array( - 'PERSON' => 'people', - 'MAN' => 'men', - 'WOMAN' => 'women', - 'CHILD' => 'children', - 'COW' => 'kine', + 'PERSON' => 'people', + 'MAN' => 'men', + 'WOMAN' => 'women', + 'CHILD' => 'children', + 'COW' => 'kine', ); static $IrregularS = array( - 'PEOPLE' => 'PERSON', - 'MEN' => 'man', - 'WOMEN' => 'woman', - 'CHILDREN' => 'child', - 'KINE' => 'cow', + 'PEOPLE' => 'PERSON', + 'MEN' => 'man', + 'WOMEN' => 'woman', + 'CHILDREN' => 'child', + 'KINE' => 'cow', ); static $WeIsI = array( 'EQUIPMENT' => true, - 'INFORMATION' => true, - 'RICE' => true, - 'MONEY' => true, - 'SPECIES' => true, - 'SERIES' => true, - 'FISH' => true, - 'SHEEP' => true, + 'INFORMATION' => true, + 'RICE' => true, + 'MONEY' => true, + 'SPECIES' => true, + 'SERIES' => true, + 'FISH' => true, + 'SHEEP' => true, ); function _pluralize($table) @@ -222,28 +224,28 @@ class ADODB_Active_Record { return $table; } $ut = strtoupper($table); - if(isset(self::$WeIsI[$ut])) { + if (isset(self::$WeIsI[$ut])) { return $table; } - if(isset(self::$IrregularP[$ut])) { + if (isset(self::$IrregularP[$ut])) { return self::$IrregularP[$ut]; } $len = strlen($table); - $lastc = $ut[$len-1]; - $lastc2 = substr($ut,$len-2); + $lastc = $ut[$len - 1]; + $lastc2 = substr($ut, $len - 2); switch ($lastc) { case 'S': - return $table.'es'; + return $table . 'es'; case 'Y': - return substr($table,0,$len-1).'ies'; + return substr($table, 0, $len - 1) . 'ies'; case 'X': - return $table.'es'; + return $table . 'es'; case 'H': if ($lastc2 == 'CH' || $lastc2 == 'SH') { - return $table.'es'; + return $table . 'es'; } default: - return $table.'s'; + return $table . 's'; } } @@ -251,36 +253,36 @@ class ADODB_Active_Record { // Note: There is an assumption here...and it is that the argument's length >= 4 function _singularize($table) { - if (!ADODB_Active_Record::$_changeNames) { - return $table; - } + return $table; + } + $ut = strtoupper($table); - if(isset(self::$WeIsI[$ut])) { + if (isset(self::$WeIsI[$ut])) { return $table; } - if(isset(self::$IrregularS[$ut])) { + if (isset(self::$IrregularS[$ut])) { return self::$IrregularS[$ut]; } $len = strlen($table); - if($ut[$len-1] != 'S') { + if ($ut[$len - 1] != 'S') { return $table; // I know...forget oxen } - if($ut[$len-2] != 'E') { - return substr($table, 0, $len-1); + if ($ut[$len - 2] != 'E') { + return substr($table, 0, $len - 1); } - switch($ut[$len-3]) { + switch ($ut[$len - 3]) { case 'S': case 'X': - return substr($table, 0, $len-2); + return substr($table, 0, $len - 2); case 'I': - return substr($table, 0, $len-3) . 'y'; + return substr($table, 0, $len - 3) . 'y'; case 'H'; - if($ut[$len-4] == 'C' || $ut[$len-4] == 'S') { - return substr($table, 0, $len-2); + if ($ut[$len - 4] == 'C' || $ut[$len - 4] == 'S') { + return substr($table, 0, $len - 2); } default: - return substr($table, 0, $len-1); // ? + return substr($table, 0, $len - 1); // ? } } @@ -292,7 +294,7 @@ class ADODB_Active_Record { * this-table.id = other-table-#1.this-table_id * = other-table-#2.this-table_id */ - function hasMany($foreignRef,$foreignKey=false) + function hasMany($foreignRef, $foreignKey = false) { $ar = new ADODB_Active_Record($foreignRef); $ar->foreignName = $foreignRef; @@ -300,7 +302,7 @@ class ADODB_Active_Record { $ar->foreignKey = ($foreignKey) ? $foreignKey : strtolower(get_class($this)) . self::$_foreignSuffix; $table =& $this->TableInfo(); - if(!isset($table->_hasMany[$foreignRef])) { + if (!isset($table->_hasMany[$foreignRef])) { $table->_hasMany[$foreignRef] = $ar; $table->updateColsCount(); } @@ -315,7 +317,7 @@ class ADODB_Active_Record { * * this-table.other-table_id = other-table.id */ - function belongsTo($foreignRef,$foreignKey=false) + function belongsTo($foreignRef, $foreignKey = false) { global $inflector; @@ -325,7 +327,7 @@ class ADODB_Active_Record { $ar->foreignKey = ($foreignKey) ? $foreignKey : $ar->foreignName . self::$_foreignSuffix; $table =& $this->TableInfo(); - if(!isset($table->_belongsTo[$foreignRef])) { + if (!isset($table->_belongsTo[$foreignRef])) { $table->_belongsTo[$foreignRef] = $ar; $table->updateColsCount(); } @@ -341,71 +343,68 @@ class ADODB_Active_Record { */ function __get($name) { - return $this->LoadRelations($name, '', -1. -1); + return $this->LoadRelations($name, '', -1. - 1); } - function LoadRelations($name, $whereOrderBy, $offset=-1, $limit=-1) + function LoadRelations($name, $whereOrderBy, $offset = -1, $limit = -1) { $extras = array(); - if($offset >= 0) { + if ($offset >= 0) { $extras['offset'] = $offset; } - if($limit >= 0) { + if ($limit >= 0) { $extras['limit'] = $limit; } $table =& $this->TableInfo(); if (strlen($whereOrderBy)) { - if (!preg_match('/^[ \n\r]*AND/i',$whereOrderBy)) { - if (!preg_match('/^[ \n\r]*ORDER[ \n\r]/i',$whereOrderBy)) { - $whereOrderBy = 'AND '.$whereOrderBy; + if (!preg_match('/^[ \n\r]*AND/i', $whereOrderBy)) { + if (!preg_match('/^[ \n\r]*ORDER[ \n\r]/i', $whereOrderBy)) { + $whereOrderBy = 'AND ' . $whereOrderBy; } } } - if(!empty($table->_belongsTo[$name])) { + if (!empty($table->_belongsTo[$name])) { $obj = $table->_belongsTo[$name]; $columnName = $obj->foreignKey; - if(empty($this->$columnName)) { + if (empty($this->$columnName)) { $this->$name = null; - } - else { - if(($k = reset($obj->TableInfo()->keys))) { + } else { + if (($k = reset($obj->TableInfo()->keys))) { $belongsToId = $k; - } - else { + } else { $belongsToId = 'id'; } $arrayOfOne = $obj->Find( - $belongsToId.'='.$this->$columnName.' '.$whereOrderBy, false, false, $extras); + $belongsToId . '=' . $this->$columnName . ' ' . $whereOrderBy, false, false, $extras); $this->$name = $arrayOfOne[0]; } return $this->$name; } - if(!empty($table->_hasMany[$name])) { + if (!empty($table->_hasMany[$name])) { $obj = $table->_hasMany[$name]; - if(($k = reset($table->keys))) { - $hasManyId = $k; - } - else { - $hasManyId = 'id'; + if (($k = reset($table->keys))) { + $hasManyId = $k; + } else { + $hasManyId = 'id'; } $this->$name = $obj->Find( - $obj->foreignKey.'='.$this->$hasManyId.' '.$whereOrderBy, false, false, $extras); + $obj->foreignKey . '=' . $this->$hasManyId . ' ' . $whereOrderBy, false, false, $extras); return $this->$name; } } ////////////////////////////////// // update metadata - function UpdateActiveTable($pkeys=false,$forceUpdate=false) + function UpdateActiveTable($pkeys = false, $forceUpdate = false) { - global $_ADODB_ACTIVE_DBS , $ADODB_CACHE_DIR, $ADODB_ACTIVE_CACHESECS; - global $ADODB_ACTIVE_DEFVALS, $ADODB_FETCH_MODE; + global $_ADODB_ACTIVE_DBS, $ADODB_CACHE_DIR, $ADODB_ACTIVE_CACHESECS; + global $ADODB_ACTIVE_DEFVALS, $ADODB_FETCH_MODE; $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; @@ -415,11 +414,10 @@ class ADODB_Active_Record { if (!$forceUpdate && !empty($tables[$tableat])) { $tobj = $tables[$tableat]; - foreach($tobj->flds as $name => $fld) { + foreach ($tobj->flds as $name => $fld) { if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value)) { $this->$name = $fld->default_value; - } - else { + } else { $this->$name = null; } } @@ -427,11 +425,11 @@ class ADODB_Active_Record { } $db = $activedb->db; - $fname = $ADODB_CACHE_DIR . '/adodb_' . $db->databaseType . '_active_'. $table . '.cache'; + $fname = $ADODB_CACHE_DIR . '/adodb_' . $db->databaseType . '_active_' . $table . '.cache'; if (!$forceUpdate && $ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR && file_exists($fname)) { - $fp = fopen($fname,'r'); + $fp = fopen($fname, 'r'); @flock($fp, LOCK_SH); - $acttab = unserialize(fread($fp,100000)); + $acttab = unserialize(fread($fp, 100000)); fclose($fp); if ($acttab->_created + $ADODB_ACTIVE_CACHESECS - (abs(rand()) % 16) > time()) { // abs(rand()) randomizes deletion, reducing contention to delete/refresh file @@ -439,7 +437,7 @@ class ADODB_Active_Record { $activedb->tables[$table] = $acttab; //if ($db->debug) ADOConnection::outp("Reading cached active record file: $fname"); - return; + return; } else if ($db->debug) { ADOConnection::outp("Refreshing cached active record file: $fname"); } @@ -461,14 +459,14 @@ class ADODB_Active_Record { $ADODB_FETCH_MODE = $save; if (!$cols) { - $this->Error("Invalid table name: $table",'UpdateActiveTable'); + $this->Error("Invalid table name: $table", 'UpdateActiveTable'); return false; } $fld = reset($cols); if (!$pkeys) { if (isset($fld->primary_key)) { $pkeys = array(); - foreach($cols as $name => $fld) { + foreach ($cols as $name => $fld) { if (!empty($fld->primary_key)) { $pkeys[] = $name; } @@ -478,7 +476,7 @@ class ADODB_Active_Record { } } if (empty($pkeys)) { - $this->Error("No primary key found for table $table",'UpdateActiveTable'); + $this->Error("No primary key found for table $table", 'UpdateActiveTable'); return false; } @@ -486,55 +484,52 @@ class ADODB_Active_Record { $keys = array(); switch (ADODB_ASSOC_CASE) { - case ADODB_ASSOC_CASE_LOWER: - foreach($cols as $name => $fldobj) { - $name = strtolower($name); - if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) { - $this->$name = $fldobj->default_value; + case ADODB_ASSOC_CASE_LOWER: + foreach ($cols as $name => $fldobj) { + $name = strtolower($name); + if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) { + $this->$name = $fldobj->default_value; + } else { + $this->$name = null; + } + $attr[$name] = $fldobj; } - else { - $this->$name = null; + foreach ($pkeys as $k => $name) { + $keys[strtolower($name)] = strtolower($name); } - $attr[$name] = $fldobj; - } - foreach($pkeys as $k => $name) { - $keys[strtolower($name)] = strtolower($name); - } - break; + break; - case ADODB_ASSOC_CASE_UPPER: - foreach($cols as $name => $fldobj) { - $name = strtoupper($name); + case ADODB_ASSOC_CASE_UPPER: + foreach ($cols as $name => $fldobj) { + $name = strtoupper($name); - if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) { - $this->$name = $fldobj->default_value; - } - else { - $this->$name = null; + if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) { + $this->$name = $fldobj->default_value; + } else { + $this->$name = null; + } + $attr[$name] = $fldobj; } - $attr[$name] = $fldobj; - } - foreach($pkeys as $k => $name) { - $keys[strtoupper($name)] = strtoupper($name); - } - break; - default: - foreach($cols as $name => $fldobj) { - $name = ($fldobj->name); + foreach ($pkeys as $k => $name) { + $keys[strtoupper($name)] = strtoupper($name); + } + break; + default: + foreach ($cols as $name => $fldobj) { + $name = ($fldobj->name); - if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) { - $this->$name = $fldobj->default_value; + if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) { + $this->$name = $fldobj->default_value; + } else { + $this->$name = null; + } + $attr[$name] = $fldobj; } - else { - $this->$name = null; + foreach ($pkeys as $k => $name) { + $keys[$name] = $cols[$name]->name; } - $attr[$name] = $fldobj; - } - foreach($pkeys as $k => $name) { - $keys[$name] = $cols[$name]->name; - } - break; + break; } $activetab->keys = $keys; @@ -545,9 +540,9 @@ class ADODB_Active_Record { $activetab->_created = time(); $s = serialize($activetab); if (!function_exists('adodb_write_file')) { - include_once(ADODB_DIR.'/adodb-csvlib.inc.php'); + include_once(ADODB_DIR . '/adodb-csvlib.inc.php'); } - adodb_write_file($fname,$s); + adodb_write_file($fname, $s); } if (isset($activedb->tables[$table])) { $oldtab = $activedb->tables[$table]; @@ -566,17 +561,16 @@ class ADODB_Active_Record { } // error handler for both PHP4+5. - function Error($err,$fn) + function Error($err, $fn) { - global $_ADODB_ACTIVE_DBS; + global $_ADODB_ACTIVE_DBS; - $fn = get_class($this).'::'.$fn; - $this->_lasterr = $fn.': '.$err; + $fn = get_class($this) . '::' . $fn; + $this->_lasterr = $fn . ': ' . $err; if ($this->_dbat < 0) { $db = false; - } - else { + } else { $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; $db = $activedb->db; } @@ -584,8 +578,7 @@ class ADODB_Active_Record { if (function_exists('adodb_throw')) { if (!$db) { adodb_throw('ADOdb_Active_Record', $fn, -1, $err, 0, 0, false); - } - else { + } else { adodb_throw($db->databaseType, $fn, -1, $err, 0, 0, $db); } } else { @@ -602,8 +595,7 @@ class ADODB_Active_Record { if (!function_exists('adodb_throw')) { if ($this->_dbat < 0) { $db = false; - } - else { + } else { $db = $this->DB(); } @@ -622,14 +614,14 @@ class ADODB_Active_Record { } $db = $this->DB(); - return (int) $db->ErrorNo(); + return (int)$db->ErrorNo(); } // retrieve ADOConnection from _ADODB_Active_DBs function DB() { - global $_ADODB_ACTIVE_DBS; + global $_ADODB_ACTIVE_DBS; if ($this->_dbat < 0) { $false = false; @@ -644,7 +636,7 @@ class ADODB_Active_Record { // retrieve ADODB_Active_Table function &TableInfo() { - global $_ADODB_ACTIVE_DBS; + global $_ADODB_ACTIVE_DBS; $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; $table = $activedb->tables[$this->_tableat]; @@ -662,14 +654,14 @@ class ADODB_Active_Record { } $table =& $this->TableInfo(); $where = $this->GenWhere($db, $table); - return($this->Load($where)); + return ($this->Load($where)); } // set a numeric array (using natural table field ordering) as object properties function Set(&$row) { - global $ACTIVE_RECORD_SAFETY; + global $ACTIVE_RECORD_SAFETY; $db = $this->DB(); @@ -682,11 +674,11 @@ class ADODB_Active_Record { $table = $this->TableInfo(); $sizeofFlds = sizeof($table->flds); - $sizeofRow = sizeof($row); + $sizeofRow = sizeof($row); if ($ACTIVE_RECORD_SAFETY && $table->_colsCount != $sizeofRow && $sizeofFlds != $sizeofRow) { # <AP> $bad_size = TRUE; - if($sizeofRow == 2 * $table->_colsCount || $sizeofRow == 2 * $sizeofFlds) { + if ($sizeofRow == 2 * $table->_colsCount || $sizeofRow == 2 * $sizeofFlds) { // Only keep string keys $keys = array_filter(array_keys($row), 'is_string'); if (sizeof($keys) == sizeof($table->flds)) { @@ -694,46 +686,45 @@ class ADODB_Active_Record { } } if ($bad_size) { - $this->Error("Table structure of $this->_table has changed","Load"); + $this->Error("Table structure of $this->_table has changed", "Load"); return false; } # </AP> - } - else { + } else { $keys = array_keys($row); } # <AP> reset($keys); $this->_original = array(); - foreach($table->flds as $name=>$fld) { + foreach ($table->flds as $name => $fld) { $value = $row[current($keys)]; $this->$name = $value; $this->_original[] = $value; - if(!next($keys)) { + if (!next($keys)) { break; } } $table =& $this->TableInfo(); - foreach($table->_belongsTo as $foreignTable) { + foreach ($table->_belongsTo as $foreignTable) { $ft = $foreignTable->TableInfo(); $propertyName = $ft->name; - foreach($ft->flds as $name=>$fld) { + foreach ($ft->flds as $name => $fld) { $value = $row[current($keys)]; $foreignTable->$name = $value; $foreignTable->_original[] = $value; - if(!next($keys)) { + if (!next($keys)) { break; } } } - foreach($table->_hasMany as $foreignTable) { + foreach ($table->_hasMany as $foreignTable) { $ft = $foreignTable->TableInfo(); - foreach($ft->flds as $name=>$fld) { + foreach ($ft->flds as $name => $fld) { $value = $row[current($keys)]; $foreignTable->$name = $value; $foreignTable->_original[] = $value; - if(!next($keys)) { + if (!next($keys)) { break; } } @@ -744,45 +735,44 @@ class ADODB_Active_Record { } // get last inserted id for INSERT - function LastInsertID(&$db,$fieldname) + function LastInsertID(&$db, $fieldname) { if ($db->hasInsertID) { - $val = $db->Insert_ID($this->_table,$fieldname); - } - else { + $val = $db->Insert_ID($this->_table, $fieldname); + } else { $val = false; } if (is_null($val) || $val === false) { // this might not work reliably in multi-user environment - return $db->GetOne("select max(".$fieldname.") from ".$this->_table); + return $db->GetOne("select max(" . $fieldname . ") from " . $this->_table); } return $val; } // quote data in where clause - function doquote(&$db, $val,$t) + function doquote(&$db, $val, $t) { - switch($t) { - case 'D': - case 'T': - if (empty($val)) { - return 'null'; - } - case 'C': - case 'X': - if (is_null($val)) { - return 'null'; - } - if (strlen($val)>0 && - (strncmp($val,"'",1) != 0 || substr($val,strlen($val)-1,1) != "'") - ) { - return $db->qstr($val); + switch ($t) { + case 'D': + case 'T': + if (empty($val)) { + return 'null'; + } + case 'C': + case 'X': + if (is_null($val)) { + return 'null'; + } + if (strlen($val) > 0 && + (strncmp($val, "'", 1) != 0 || substr($val, strlen($val) - 1, 1) != "'") + ) { + return $db->qstr($val); + break; + } + default: + return $val; break; - } - default: - return $val; - break; } } @@ -792,10 +782,10 @@ class ADODB_Active_Record { $keys = $table->keys; $parr = array(); - foreach($keys as $k) { + foreach ($keys as $k) { $f = $table->flds[$k]; if ($f) { - $parr[] = $k.' = '.$this->doquote($db,$this->$k,$db->MetaType($f->type)); + $parr[] = $k . ' = ' . $this->doquote($db, $this->$k, $db->MetaType($f->type)); } } return implode(' and ', $parr); @@ -804,7 +794,7 @@ class ADODB_Active_Record { //------------------------------------------------------------ Public functions below - function Load($where=null,$bindarr=false) + function Load($where = null, $bindarr = false) { $db = $this->DB(); if (!$db) { @@ -813,41 +803,38 @@ class ADODB_Active_Record { $this->_where = $where; $save = $db->SetFetchMode(ADODB_FETCH_NUM); - $qry = "select * from ".$this->_table; + $qry = "select * from " . $this->_table; $table =& $this->TableInfo(); - if(($k = reset($table->keys))) { - $hasManyId = $k; - } - else { - $hasManyId = 'id'; + if (($k = reset($table->keys))) { + $hasManyId = $k; + } else { + $hasManyId = 'id'; } - foreach($table->_belongsTo as $foreignTable) { - if(($k = reset($foreignTable->TableInfo()->keys))) { + foreach ($table->_belongsTo as $foreignTable) { + if (($k = reset($foreignTable->TableInfo()->keys))) { $belongsToId = $k; - } - else { + } else { $belongsToId = 'id'; } - $qry .= ' LEFT JOIN '.$foreignTable->_table.' ON '. - $this->_table.'.'.$foreignTable->foreignKey.'='. - $foreignTable->_table.'.'.$belongsToId; + $qry .= ' LEFT JOIN ' . $foreignTable->_table . ' ON ' . + $this->_table . '.' . $foreignTable->foreignKey . '=' . + $foreignTable->_table . '.' . $belongsToId; } - foreach($table->_hasMany as $foreignTable) - { - $qry .= ' LEFT JOIN '.$foreignTable->_table.' ON '. - $this->_table.'.'.$hasManyId.'='. - $foreignTable->_table.'.'.$foreignTable->foreignKey; + foreach ($table->_hasMany as $foreignTable) { + $qry .= ' LEFT JOIN ' . $foreignTable->_table . ' ON ' . + $this->_table . '.' . $hasManyId . '=' . + $foreignTable->_table . '.' . $foreignTable->foreignKey; } - if($where) { - $qry .= ' WHERE '.$where; + if ($where) { + $qry .= ' WHERE ' . $where; } // Simple case: no relations. Load row and return. - if((count($table->_hasMany) + count($table->_belongsTo)) < 1) { - $row = $db->GetRow($qry,$bindarr); - if(!$row) { + if ((count($table->_hasMany) + count($table->_belongsTo)) < 1) { + $row = $db->GetRow($qry, $bindarr); + if (!$row) { return false; } $db->SetFetchMode($save); @@ -855,73 +842,71 @@ class ADODB_Active_Record { } // More complex case when relations have to be collated - $rows = $db->GetAll($qry,$bindarr); - if(!$rows) { + $rows = $db->GetAll($qry, $bindarr); + if (!$rows) { return false; } $db->SetFetchMode($save); - if(count($rows) < 1) { + if (count($rows) < 1) { return false; } $class = get_class($this); $isFirstRow = true; - if(($k = reset($this->TableInfo()->keys))) { - $myId = $k; - } - else { - $myId = 'id'; + if (($k = reset($this->TableInfo()->keys))) { + $myId = $k; + } else { + $myId = 'id'; } - $index = 0; $found = false; + $index = 0; + $found = false; /** @todo Improve by storing once and for all in table metadata */ /** @todo Also re-use info for hasManyId */ - foreach($this->TableInfo()->flds as $fld) { - if($fld->name == $myId) { + foreach ($this->TableInfo()->flds as $fld) { + if ($fld->name == $myId) { $found = true; break; } $index++; } - if(!$found) { - $this->outp_throw("Unable to locate key $myId for $class in Load()",'Load'); + if (!$found) { + $this->outp_throw("Unable to locate key $myId for $class in Load()", 'Load'); } - foreach($rows as $row) { + foreach ($rows as $row) { $rowId = intval($row[$index]); - if($rowId > 0) { - if($isFirstRow) { + if ($rowId > 0) { + if ($isFirstRow) { $isFirstRow = false; - if(!$this->Set($row)) { + if (!$this->Set($row)) { return false; } } - $obj = new $class($table,false,$db); + $obj = new $class($table, false, $db); $obj->Set($row); // TODO Copy/paste code below: bad! - if(count($table->_hasMany) > 0) { - foreach($table->_hasMany as $foreignTable) { + if (count($table->_hasMany) > 0) { + foreach ($table->_hasMany as $foreignTable) { $foreignName = $foreignTable->foreignName; - if(!empty($obj->$foreignName)) { - if(!is_array($this->$foreignName)) { + if (!empty($obj->$foreignName)) { + if (!is_array($this->$foreignName)) { $foreignObj = $this->$foreignName; $this->$foreignName = array(clone($foreignObj)); - } - else { + } else { $foreignObj = $obj->$foreignName; array_push($this->$foreignName, clone($foreignObj)); } } } } - if(count($table->_belongsTo) > 0) { - foreach($table->_belongsTo as $foreignTable) { + if (count($table->_belongsTo) > 0) { + foreach ($table->_belongsTo as $foreignTable) { $foreignName = $foreignTable->foreignName; - if(!empty($obj->$foreignName)) { - if(!is_array($this->$foreignName)) { + if (!empty($obj->$foreignName)) { + if (!is_array($this->$foreignName)) { $foreignObj = $this->$foreignName; $this->$foreignName = array(clone($foreignObj)); - } - else { + } else { $foreignObj = $obj->$foreignName; array_push($this->$foreignName, clone($foreignObj)); } @@ -938,8 +923,7 @@ class ADODB_Active_Record { { if ($this->_saved) { $ok = $this->Update(); - } - else { + } else { $ok = $this->Insert(); } @@ -967,9 +951,9 @@ class ADODB_Active_Record { $names = array(); $valstr = array(); - foreach($table->flds as $name=>$fld) { + foreach ($table->flds as $name => $fld) { $val = $this->$name; - if(!is_null($val) || !array_key_exists($name, $table->keys)) { + if (!is_null($val) || !array_key_exists($name, $table->keys)) { $valarr[] = $val; $names[] = $name; $valstr[] = $db->Param($cnt); @@ -977,21 +961,21 @@ class ADODB_Active_Record { } } - if (empty($names)){ - foreach($table->flds as $name=>$fld) { + if (empty($names)) { + foreach ($table->flds as $name => $fld) { $valarr[] = null; $names[] = $name; $valstr[] = $db->Param($cnt); $cnt += 1; } } - $sql = 'INSERT INTO '.$this->_table."(".implode(',',$names).') VALUES ('.implode(',',$valstr).')'; - $ok = $db->Execute($sql,$valarr); + $sql = 'INSERT INTO ' . $this->_table . "(" . implode(',', $names) . ') VALUES (' . implode(',', $valstr) . ')'; + $ok = $db->Execute($sql, $valarr); if ($ok) { $this->_saved = true; $autoinc = false; - foreach($table->keys as $k) { + foreach ($table->keys as $k) { if (is_null($this->$k)) { $autoinc = true; break; @@ -999,7 +983,7 @@ class ADODB_Active_Record { } if ($autoinc && sizeof($table->keys) == 1) { $k = reset($table->keys); - $this->$k = $this->LastInsertID($db,$k); + $this->$k = $this->LastInsertID($db, $k); } } @@ -1015,23 +999,23 @@ class ADODB_Active_Record { } $table = $this->TableInfo(); - $where = $this->GenWhere($db,$table); - $sql = 'DELETE FROM '.$this->_table.' WHERE '.$where; + $where = $this->GenWhere($db, $table); + $sql = 'DELETE FROM ' . $this->_table . ' WHERE ' . $where; $ok = $db->Execute($sql); return $ok ? true : false; } // returns an array of active record objects - function Find($whereOrderBy,$bindarr=false,$pkeysArr=false,$extra=array()) + function Find($whereOrderBy, $bindarr = false, $pkeysArr = false, $extra = array()) { $db = $this->DB(); if (!$db || empty($this->_table)) { return false; } $table =& $this->TableInfo(); - $arr = $db->GetActiveRecordsClass(get_class($this),$this, $whereOrderBy,$bindarr,$pkeysArr,$extra, - array('foreignName'=>$this->foreignName, 'belongsTo'=>$table->_belongsTo, 'hasMany'=>$table->_hasMany)); + $arr = $db->GetActiveRecordsClass(get_class($this), $this, $whereOrderBy, $bindarr, $pkeysArr, $extra, + array('foreignName' => $this->foreignName, 'belongsTo' => $table->_belongsTo, 'hasMany' => $table->_hasMany)); return $arr; } @@ -1039,15 +1023,15 @@ class ADODB_Active_Record { // subclasses...for instance when GetActiveRecordsClass invokes Find() // Why am I not invoking parent::Find? // Shockingly because I want to preserve PHP4 compatibility. - function packageFind($whereOrderBy,$bindarr=false,$pkeysArr=false,$extra=array()) + function packageFind($whereOrderBy, $bindarr = false, $pkeysArr = false, $extra = array()) { $db = $this->DB(); if (!$db || empty($this->_table)) { return false; } $table =& $this->TableInfo(); - $arr = $db->GetActiveRecordsClass(get_class($this),$this, $whereOrderBy,$bindarr,$pkeysArr,$extra, - array('foreignName'=>$this->foreignName, 'belongsTo'=>$table->_belongsTo, 'hasMany'=>$table->_hasMany)); + $arr = $db->GetActiveRecordsClass(get_class($this), $this, $whereOrderBy, $bindarr, $pkeysArr, $extra, + array('foreignName' => $this->foreignName, 'belongsTo' => $table->_belongsTo, 'hasMany' => $table->_hasMany)); return $arr; } @@ -1062,7 +1046,7 @@ class ADODB_Active_Record { $pkey = $table->keys; - foreach($table->flds as $name=>$fld) { + foreach ($table->flds as $name => $fld) { $val = $this->$name; /* if (is_null($val)) { @@ -1080,7 +1064,7 @@ class ADODB_Active_Record { continue; } $t = $db->MetaType($fld->type); - $arr[$name] = $this->doquote($db,$val,$t); + $arr[$name] = $this->doquote($db, $val, $t); $valarr[] = $val; } @@ -1091,23 +1075,23 @@ class ADODB_Active_Record { switch (ADODB_ASSOC_CASE) { case ADODB_ASSOC_CASE_LOWER: - foreach($pkey as $k => $v) { + foreach ($pkey as $k => $v) { $pkey[$k] = strtolower($v); } break; case ADODB_ASSOC_CASE_UPPER: - foreach($pkey as $k => $v) { + foreach ($pkey as $k => $v) { $pkey[$k] = strtoupper($v); } break; } - $ok = $db->Replace($this->_table,$arr,$pkey); + $ok = $db->Replace($this->_table, $arr, $pkey); if ($ok) { $this->_saved = true; // 1= update 2=insert if ($ok == 2) { $autoinc = false; - foreach($table->keys as $k) { + foreach ($table->keys as $k) { if (is_null($this->$k)) { $autoinc = true; break; @@ -1115,7 +1099,7 @@ class ADODB_Active_Record { } if ($autoinc && sizeof($table->keys) == 1) { $k = reset($table->keys); - $this->$k = $this->LastInsertID($db,$k); + $this->$k = $this->LastInsertID($db, $k); } } @@ -1144,7 +1128,7 @@ class ADODB_Active_Record { $pairs = array(); $i = 0; $cnt = 0; - foreach($table->flds as $name=>$fld) { + foreach ($table->flds as $name => $fld) { $orig = $this->_original[$i++] ?? null; $val = $this->$name; $neworig[] = $val; @@ -1157,9 +1141,8 @@ class ADODB_Active_Record { if (isset($fld->not_null) && $fld->not_null) { if (isset($fld->default_value) && strlen($fld->default_value)) { continue; - } - else { - $this->Error("Cannot set field $name to NULL","Update"); + } else { + $this->Error("Cannot set field $name to NULL", "Update"); return false; } } @@ -1168,17 +1151,17 @@ class ADODB_Active_Record { if ($val === $orig) { continue; } + $valarr[] = $val; - $pairs[] = $name.'='.$db->Param($cnt); + $pairs[] = $name . '=' . $db->Param($cnt); $cnt += 1; } - if (!$cnt) { return -1; } - $sql = 'UPDATE '.$this->_table." SET ".implode(",",$pairs)." WHERE ".$where; - $ok = $db->Execute($sql,$valarr); + $sql = 'UPDATE ' . $this->_table . " SET " . implode(",", $pairs) . " WHERE " . $where; + $ok = $db->Execute($sql, $valarr); if ($ok) { $this->_original = $neworig; return 1; |
