- Timestamp:
- 09/26/13 15:41:49 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sandbox/2.5.1-evolucao/phpgwapi/inc/adodb/adodb-active-record.inc.php
r34 r8222 2 2 /* 3 3 4 @version V 4.94 23 Jan 2007 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.4 @version V5.18 3 Sep 2012 (c) 2000-2012 John Lim (jlim#natsoft.com). All rights reserved. 5 5 Latest version is available at http://adodb.sourceforge.net 6 6 … … 11 11 Active Record implementation. Superset of Zend Framework's. 12 12 13 Version 0. 0713 Version 0.92 14 14 15 15 See http://www-128.ibm.com/developerworks/java/library/j-cb03076/?ca=dgr-lnxw01ActiveRecord … … 17 17 */ 18 18 19 19 20 global $_ADODB_ACTIVE_DBS; 20 21 global $ADODB_ACTIVE_CACHESECS; // set to true to enable caching of metadata such as field info 21 22 global $ACTIVE_RECORD_SAFETY; // set to false to disable safety checks 23 global $ADODB_ACTIVE_DEFVALS; // use default values of table definition when creating new active record. 22 24 23 25 // array of ADODB_Active_DB's, indexed by ADODB_Active_Record->_dbat 24 26 $_ADODB_ACTIVE_DBS = array(); 25 27 $ACTIVE_RECORD_SAFETY = true; 28 $ADODB_ACTIVE_DEFVALS = false; 29 $ADODB_ACTIVE_CACHESECS = 0; 26 30 27 31 class ADODB_Active_DB { … … 35 39 var $keys; // assoc array of primary keys, indexed by fieldname 36 40 var $_created; // only used when stored as a cached file 41 var $_belongsTo = array(); 42 var $_hasMany = array(); 37 43 } 38 44 45 // $db = database connection 46 // $index = name of index - can be associative, for an example see 47 // http://phplens.com/lens/lensforum/msgs.php?id=17790 39 48 // returns index into $_ADODB_ACTIVE_DBS 40 function ADODB_SetDatabaseAdapter(&$db )49 function ADODB_SetDatabaseAdapter(&$db, $index=false) 41 50 { 42 51 global $_ADODB_ACTIVE_DBS; … … 52 61 53 62 $obj = new ADODB_Active_DB(); 54 $obj->db = &$db;63 $obj->db = $db; 55 64 $obj->tables = array(); 56 65 57 $_ADODB_ACTIVE_DBS[] = $obj; 66 if ($index == false) $index = sizeof($_ADODB_ACTIVE_DBS); 67 68 69 $_ADODB_ACTIVE_DBS[$index] = $obj; 58 70 59 71 return sizeof($_ADODB_ACTIVE_DBS)-1; … … 62 74 63 75 class ADODB_Active_Record { 76 static $_changeNames = true; // dynamically pluralize table names 77 static $_quoteNames = false; 78 79 static $_foreignSuffix = '_id'; // 64 80 var $_dbat; // associative index pointing to ADODB_Active_DB eg. $ADODB_Active_DBS[_dbat] 65 81 var $_table; // tablename, if set in class definition then use it as table name … … 69 85 var $_lasterr = false; // last error message 70 86 var $_original = false; // the original values loaded or inserted, refreshed on update 71 87 88 var $foreignName; // CFR: class name when in a relationship 89 90 var $lockMode = ' for update '; // you might want to change to 91 92 static function UseDefaultValues($bool=null) 93 { 94 global $ADODB_ACTIVE_DEFVALS; 95 if (isset($bool)) $ADODB_ACTIVE_DEFVALS = $bool; 96 return $ADODB_ACTIVE_DEFVALS; 97 } 98 72 99 // should be static 73 function SetDatabaseAdapter(&$db) 74 { 75 return ADODB_SetDatabaseAdapter($db); 76 } 77 78 // php4 constructor 79 function ADODB_Active_Record($table = false, $pkeyarr=false, $db=false) 80 { 81 ADODB_Active_Record::__construct($table,$pkeyarr,$db); 100 static function SetDatabaseAdapter(&$db, $index=false) 101 { 102 return ADODB_SetDatabaseAdapter($db, $index); 103 } 104 105 106 public function __set($name, $value) 107 { 108 $name = str_replace(' ', '_', $name); 109 $this->$name = $value; 82 110 } 83 111 … … 96 124 else $table = $this->_pluralize(get_class($this)); 97 125 } 126 $this->foreignName = strtolower(get_class($this)); // CFR: default foreign name 98 127 if ($db) { 99 128 $this->_dbat = ADODB_Active_Record::SetDatabaseAdapter($db); 100 } else 101 $this->_dbat = sizeof($_ADODB_ACTIVE_DBS)-1;102 103 104 if ($this->_dbat < 0) $this->Error("No database connection set; use ADOdb_Active_Record::SetDatabaseAdapter(\$db)",'ADODB_Active_Record::__constructor');105 129 } else if (!isset($this->_dbat)) { 130 if (sizeof($_ADODB_ACTIVE_DBS) == 0) $this->Error("No database connection set; use ADOdb_Active_Record::SetDatabaseAdapter(\$db)",'ADODB_Active_Record::__constructor'); 131 end($_ADODB_ACTIVE_DBS); 132 $this->_dbat = key($_ADODB_ACTIVE_DBS); 133 } 134 106 135 $this->_table = $table; 107 136 $this->_tableat = $table; # reserved for setting the assoc value to a non-table name, eg. the sql string in future 137 108 138 $this->UpdateActiveTable($pkeyarr); 109 139 } … … 117 147 function _pluralize($table) 118 148 { 149 if (!ADODB_Active_Record::$_changeNames) return $table; 150 119 151 $ut = strtoupper($table); 120 152 $len = strlen($table); … … 136 168 } 137 169 170 // CFR Lamest singular inflector ever - @todo Make it real! 171 // Note: There is an assumption here...and it is that the argument's length >= 4 172 function _singularize($tables) 173 { 174 175 if (!ADODB_Active_Record::$_changeNames) return $table; 176 177 $ut = strtoupper($tables); 178 $len = strlen($tables); 179 if($ut[$len-1] != 'S') 180 return $tables; // I know...forget oxen 181 if($ut[$len-2] != 'E') 182 return substr($tables, 0, $len-1); 183 switch($ut[$len-3]) 184 { 185 case 'S': 186 case 'X': 187 return substr($tables, 0, $len-2); 188 case 'I': 189 return substr($tables, 0, $len-3) . 'y'; 190 case 'H'; 191 if($ut[$len-4] == 'C' || $ut[$len-4] == 'S') 192 return substr($tables, 0, $len-2); 193 default: 194 return substr($tables, 0, $len-1); // ? 195 } 196 } 197 198 function hasMany($foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record') 199 { 200 $ar = new $foreignClass($foreignRef); 201 $ar->foreignName = $foreignRef; 202 $ar->UpdateActiveTable(); 203 $ar->foreignKey = ($foreignKey) ? $foreignKey : $foreignRef.ADODB_Active_Record::$_foreignSuffix; 204 $table =& $this->TableInfo(); 205 $table->_hasMany[$foreignRef] = $ar; 206 # $this->$foreignRef = $this->_hasMany[$foreignRef]; // WATCHME Removed assignment by ref. to please __get() 207 } 208 209 // use when you don't want ADOdb to auto-pluralize tablename 210 static function TableHasMany($table, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record') 211 { 212 $ar = new ADODB_Active_Record($table); 213 $ar->hasMany($foreignRef, $foreignKey, $foreignClass); 214 } 215 216 // use when you don't want ADOdb to auto-pluralize tablename 217 static function TableKeyHasMany($table, $tablePKey, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record') 218 { 219 if (!is_array($tablePKey)) $tablePKey = array($tablePKey); 220 $ar = new ADODB_Active_Record($table,$tablePKey); 221 $ar->hasMany($foreignRef, $foreignKey, $foreignClass); 222 } 223 224 225 // use when you want ADOdb to auto-pluralize tablename for you. Note that the class must already be defined. 226 // e.g. class Person will generate relationship for table Persons 227 static function ClassHasMany($parentclass, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record') 228 { 229 $ar = new $parentclass(); 230 $ar->hasMany($foreignRef, $foreignKey, $foreignClass); 231 } 232 233 234 function belongsTo($foreignRef,$foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record') 235 { 236 global $inflector; 237 238 $ar = new $parentClass($this->_pluralize($foreignRef)); 239 $ar->foreignName = $foreignRef; 240 $ar->parentKey = $parentKey; 241 $ar->UpdateActiveTable(); 242 $ar->foreignKey = ($foreignKey) ? $foreignKey : $foreignRef.ADODB_Active_Record::$_foreignSuffix; 243 244 $table =& $this->TableInfo(); 245 $table->_belongsTo[$foreignRef] = $ar; 246 # $this->$foreignRef = $this->_belongsTo[$foreignRef]; 247 } 248 249 static function ClassBelongsTo($class, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record') 250 { 251 $ar = new $class(); 252 $ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass); 253 } 254 255 static function TableBelongsTo($table, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record') 256 { 257 $ar = new ADOdb_Active_Record($table); 258 $ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass); 259 } 260 261 static function TableKeyBelongsTo($table, $tablePKey, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record') 262 { 263 if (!is_array($tablePKey)) $tablePKey = array($tablePKey); 264 $ar = new ADOdb_Active_Record($table, $tablePKey); 265 $ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass); 266 } 267 268 269 /** 270 * __get Access properties - used for lazy loading 271 * 272 * @param mixed $name 273 * @access protected 274 * @return mixed 275 */ 276 function __get($name) 277 { 278 return $this->LoadRelations($name, '', -1, -1); 279 } 280 281 /** 282 * @param string $name 283 * @param string $whereOrderBy : eg. ' AND field1 = value ORDER BY field2' 284 * @param offset 285 * @param limit 286 * @return mixed 287 */ 288 function LoadRelations($name, $whereOrderBy='', $offset=-1,$limit=-1) 289 { 290 $extras = array(); 291 $table = $this->TableInfo(); 292 if ($limit >= 0) $extras['limit'] = $limit; 293 if ($offset >= 0) $extras['offset'] = $offset; 294 295 if (strlen($whereOrderBy)) 296 if (!preg_match('/^[ \n\r]*AND/i',$whereOrderBy)) 297 if (!preg_match('/^[ \n\r]*ORDER[ \n\r]/i',$whereOrderBy)) 298 $whereOrderBy = 'AND '.$whereOrderBy; 299 300 if(!empty($table->_belongsTo[$name])) 301 { 302 $obj = $table->_belongsTo[$name]; 303 $columnName = $obj->foreignKey; 304 if(empty($this->$columnName)) 305 $this->$name = null; 306 else 307 { 308 if ($obj->parentKey) $key = $obj->parentKey; 309 else $key = reset($table->keys); 310 311 $arrayOfOne = $obj->Find($key.'='.$this->$columnName.' '.$whereOrderBy,false,false,$extras); 312 if ($arrayOfOne) { 313 $this->$name = $arrayOfOne[0]; 314 return $arrayOfOne[0]; 315 } 316 } 317 } 318 if(!empty($table->_hasMany[$name])) 319 { 320 $obj = $table->_hasMany[$name]; 321 $key = reset($table->keys); 322 $id = @$this->$key; 323 if (!is_numeric($id)) { 324 $db = $this->DB(); 325 $id = $db->qstr($id); 326 } 327 $objs = $obj->Find($obj->foreignKey.'='.$id. ' '.$whereOrderBy,false,false,$extras); 328 if (!$objs) $objs = array(); 329 $this->$name = $objs; 330 return $objs; 331 } 332 333 return array(); 334 } 138 335 ////////////////////////////////// 139 336 … … 142 339 { 143 340 global $ADODB_ASSOC_CASE,$_ADODB_ACTIVE_DBS , $ADODB_CACHE_DIR, $ADODB_ACTIVE_CACHESECS; 144 145 $activedb =& $_ADODB_ACTIVE_DBS[$this->_dbat]; 341 global $ADODB_ACTIVE_DEFVALS,$ADODB_FETCH_MODE; 342 343 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; 146 344 147 345 $table = $this->_table; … … 149 347 $tableat = $this->_tableat; 150 348 if (!$forceUpdate && !empty($tables[$tableat])) { 151 $tobj =& $tables[$tableat]; 152 foreach($tobj->flds as $name => $fld) 349 350 $acttab = $tables[$tableat]; 351 foreach($acttab->flds as $name => $fld) { 352 if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value)) 353 $this->$name = $fld->default_value; 354 else 153 355 $this->$name = null; 356 } 154 357 return; 155 358 } 156 157 $db =& $activedb->db; 359 $db = $activedb->db; 158 360 $fname = $ADODB_CACHE_DIR . '/adodb_' . $db->databaseType . '_active_'. $table . '.cache'; 159 361 if (!$forceUpdate && $ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR && file_exists($fname)) { … … 165 367 // abs(rand()) randomizes deletion, reducing contention to delete/refresh file 166 368 // ideally, you should cache at least 32 secs 369 370 foreach($acttab->flds as $name => $fld) { 371 if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value)) 372 $this->$name = $fld->default_value; 373 else 374 $this->$name = null; 375 } 376 167 377 $activedb->tables[$table] = $acttab; 168 378 … … 176 386 $activetab->name = $table; 177 387 388 $save = $ADODB_FETCH_MODE; 389 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; 390 if ($db->fetchMode !== false) $savem = $db->SetFetchMode(false); 178 391 179 392 $cols = $db->MetaColumns($table); 393 394 if (isset($savem)) $db->SetFetchMode($savem); 395 $ADODB_FETCH_MODE = $save; 396 180 397 if (!$cols) { 181 398 $this->Error("Invalid table name: $table",'UpdateActiveTable'); … … 204 421 foreach($cols as $name => $fldobj) { 205 422 $name = strtolower($name); 206 $this->$name = null; 423 if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) 424 $this->$name = $fldobj->default_value; 425 else 426 $this->$name = null; 207 427 $attr[$name] = $fldobj; 208 428 } … … 215 435 foreach($cols as $name => $fldobj) { 216 436 $name = strtoupper($name); 217 $this->$name = null; 437 438 if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) 439 $this->$name = $fldobj->default_value; 440 else 441 $this->$name = null; 218 442 $attr[$name] = $fldobj; 219 443 } … … 226 450 foreach($cols as $name => $fldobj) { 227 451 $name = ($fldobj->name); 228 $this->$name = null; 452 453 if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) 454 $this->$name = $fldobj->default_value; 455 else 456 $this->$name = null; 229 457 $attr[$name] = $fldobj; 230 458 } … … 244 472 adodb_write_file($fname,$s); 245 473 } 474 if (isset($activedb->tables[$table])) { 475 $oldtab = $activedb->tables[$table]; 476 477 if ($oldtab) $activetab->_belongsTo = $oldtab->_belongsTo; 478 if ($oldtab) $activetab->_hasMany = $oldtab->_hasMany; 479 } 246 480 $activedb->tables[$table] = $activetab; 247 481 } … … 263 497 else { 264 498 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; 265 $db = &$activedb->db;499 $db = $activedb->db; 266 500 } 267 501 … … 297 531 298 532 // retrieve ADOConnection from _ADODB_Active_DBs 299 function &DB()533 function DB() 300 534 { 301 535 global $_ADODB_ACTIVE_DBS; … … 307 541 } 308 542 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; 309 $db = &$activedb->db;543 $db = $activedb->db; 310 544 return $db; 311 545 } … … 315 549 { 316 550 global $_ADODB_ACTIVE_DBS; 317 318 551 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; 319 $table = &$activedb->tables[$this->_tableat];552 $table = $activedb->tables[$this->_tableat]; 320 553 return $table; 321 554 } 555 556 557 // I have an ON INSERT trigger on a table that sets other columns in the table. 558 // So, I find that for myTable, I want to reload an active record after saving it. -- Malcolm Cook 559 function Reload() 560 { 561 $db =& $this->DB(); if (!$db) return false; 562 $table =& $this->TableInfo(); 563 $where = $this->GenWhere($db, $table); 564 return($this->Load($where)); 565 } 566 322 567 323 568 // set a numeric array (using natural table field ordering) as object properties … … 326 571 global $ACTIVE_RECORD_SAFETY; 327 572 328 $db = &$this->DB();573 $db = $this->DB(); 329 574 330 575 if (!$row) { … … 335 580 $this->_saved = true; 336 581 337 $table = &$this->TableInfo();582 $table = $this->TableInfo(); 338 583 if ($ACTIVE_RECORD_SAFETY && sizeof($table->flds) != sizeof($row)) { 584 # <AP> 585 $bad_size = TRUE; 586 if (sizeof($row) == 2 * sizeof($table->flds)) { 587 // Only keep string keys 588 $keys = array_filter(array_keys($row), 'is_string'); 589 if (sizeof($keys) == sizeof($table->flds)) 590 $bad_size = FALSE; 591 } 592 if ($bad_size) { 339 593 $this->Error("Table structure of $this->_table has changed","Load"); 340 594 return false; 341 595 } 342 343 $cnt = 0; 596 # </AP> 597 } 598 else 599 $keys = array_keys($row); 600 601 # <AP> 602 reset($keys); 603 $this->_original = array(); 344 604 foreach($table->flds as $name=>$fld) { 345 $this->$name = $row[$cnt]; 346 $cnt += 1; 347 } 348 $this->_original = $row; 605 $value = $row[current($keys)]; 606 $this->$name = $value; 607 $this->_original[] = $value; 608 next($keys); 609 } 610 611 # </AP> 349 612 return true; 350 613 } … … 369 632 { 370 633 switch($t) { 371 case 'D': 634 case 'L': 635 if (strpos($db->databaseType,'postgres') !== false) return $db->qstr($val); 636 case 'D': 372 637 case 'T': 373 638 if (empty($val)) return 'null'; 374 639 640 case 'B': 641 case 'N': 375 642 case 'C': 376 643 case 'X': 377 644 if (is_null($val)) return 'null'; 378 645 379 if (strncmp($val,"'",1) != 0 && substr($val,strlen($val)-1,1) != "'") { 646 if (strlen($val)>0 && 647 (strncmp($val,"'",1) != 0 || substr($val,strlen($val)-1,1) != "'")) { 380 648 return $db->qstr($val); 381 649 break; … … 403 671 404 672 673 function _QName($n,$db=false) 674 { 675 if (!ADODB_Active_Record::$_quoteNames) return $n; 676 if (!$db) $db = $this->DB(); if (!$db) return false; 677 return $db->nameQuote.$n.$db->nameQuote; 678 } 679 405 680 //------------------------------------------------------------ Public functions below 406 681 407 function Load($where,$bindarr=false) 408 { 409 $db =& $this->DB(); if (!$db) return false; 682 function Load($where=null,$bindarr=false, $lock = false) 683 { 684 global $ADODB_FETCH_MODE; 685 686 $db = $this->DB(); if (!$db) return false; 410 687 $this->_where = $where; 411 688 412 $save = $db->SetFetchMode(ADODB_FETCH_NUM); 413 $row = $db->GetRow("select * from ".$this->_table.' WHERE '.$where,$bindarr); 414 $db->SetFetchMode($save); 689 $save = $ADODB_FETCH_MODE; 690 $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 691 if ($db->fetchMode !== false) $savem = $db->SetFetchMode(false); 692 693 $qry = "select * from ".$this->_table; 694 695 if($where) { 696 $qry .= ' WHERE '.$where; 697 } 698 if ($lock) $qry .= $this->lockMode; 699 700 $row = $db->GetRow($qry,$bindarr); 701 702 if (isset($savem)) $db->SetFetchMode($savem); 703 $ADODB_FETCH_MODE = $save; 415 704 416 705 return $this->Set($row); 417 706 } 707 708 function LoadLocked($where=null, $bindarr=false) 709 { 710 $this->Load($where,$bindarr,true); 711 } 712 713 # useful for multiple record inserts 714 # see http://phplens.com/lens/lensforum/msgs.php?id=17795 715 function Reset() 716 { 717 $this->_where=null; 718 $this->_saved = false; 719 $this->_lasterr = false; 720 $this->_original = false; 721 $vars=get_object_vars($this); 722 foreach($vars as $k=>$v){ 723 if(substr($k,0,1)!=='_'){ 724 $this->{$k}=null; 725 } 726 } 727 $this->foreignName=strtolower(get_class($this)); 728 return true; 729 } 418 730 419 731 // false on error … … 426 738 } 427 739 740 428 741 // false on error 429 742 function Insert() 430 743 { 431 $db = &$this->DB(); if (!$db) return false;744 $db = $this->DB(); if (!$db) return false; 432 745 $cnt = 0; 433 $table = &$this->TableInfo();746 $table = $this->TableInfo(); 434 747 435 748 $valarr = array(); … … 439 752 foreach($table->flds as $name=>$fld) { 440 753 $val = $this->$name; 441 if(!is_ null($val) || !array_key_exists($name, $table->keys)) {754 if(!is_array($val) || !is_null($val) || !array_key_exists($name, $table->keys)) { 442 755 $valarr[] = $val; 443 $names[] = $ name;756 $names[] = $this->_QName($name,$db); 444 757 $valstr[] = $db->Param($cnt); 445 758 $cnt += 1; … … 479 792 function Delete() 480 793 { 481 $db = &$this->DB(); if (!$db) return false;482 $table = &$this->TableInfo();794 $db = $this->DB(); if (!$db) return false; 795 $table = $this->TableInfo(); 483 796 484 797 $where = $this->GenWhere($db,$table); … … 490 803 491 804 // returns an array of active record objects 492 function &Find($whereOrderBy,$bindarr=false,$pkeysArr=false)493 { 494 $db = &$this->DB(); if (!$db || empty($this->_table)) return false;495 $arr = & $db->GetActiveRecordsClass(get_class($this),$this->_table, $whereOrderBy,$bindarr,$pkeysArr);805 function Find($whereOrderBy,$bindarr=false,$pkeysArr=false,$extra=array()) 806 { 807 $db = $this->DB(); if (!$db || empty($this->_table)) return false; 808 $arr = $db->GetActiveRecordsClass(get_class($this),$this->_table, $whereOrderBy,$bindarr,$pkeysArr,$extra); 496 809 return $arr; 497 810 } … … 502 815 global $ADODB_ASSOC_CASE; 503 816 504 $db = &$this->DB(); if (!$db) return false;505 $table = &$this->TableInfo();817 $db = $this->DB(); if (!$db) return false; 818 $table = $this->TableInfo(); 506 819 507 820 $pkey = $table->keys; … … 522 835 continue; 523 836 } 837 838 if (is_array($val)) continue; 839 524 840 $t = $db->MetaType($fld->type); 525 841 $arr[$name] = $this->doquote($db,$val,$t); … … 533 849 foreach($pkey as $k => $v) 534 850 $pkey[$k] = strtolower($v); 535 elseif ($ADODB_ASSOC_CASE == 0)851 elseif ($ADODB_ASSOC_CASE == 1) 536 852 foreach($pkey as $k => $v) 537 853 $pkey[$k] = strtoupper($v); … … 554 870 } 555 871 556 $this->_original = &$valarr;872 $this->_original = $valarr; 557 873 } 558 874 return $ok; … … 562 878 function Update() 563 879 { 564 $db = &$this->DB(); if (!$db) return false;565 $table = &$this->TableInfo();880 $db = $this->DB(); if (!$db) return false; 881 $table = $this->TableInfo(); 566 882 567 883 $where = $this->GenWhere($db, $table); … … 581 897 $neworig[] = $val; 582 898 583 if (isset($table->keys[$name]) ) {899 if (isset($table->keys[$name]) || is_array($val)) 584 900 continue; 585 }586 901 587 902 if (is_null($val)) { … … 594 909 } 595 910 } 596 597 if (isset($this->_original[$i]) && $val == $this->_original[$i]) { 598 continue; 599 } 911 912 if (isset($this->_original[$i]) && strcmp($val,$this->_original[$i]) == 0) continue; 913 914 if (is_null($this->_original[$i]) && is_null($val)) continue; 915 600 916 $valarr[] = $val; 601 $pairs[] = $ name.'='.$db->Param($cnt);917 $pairs[] = $this->_QName($name,$db).'='.$db->Param($cnt); 602 918 $cnt += 1; 603 919 } … … 608 924 $ok = $db->Execute($sql,$valarr); 609 925 if ($ok) { 610 $this->_original = &$neworig;926 $this->_original = $neworig; 611 927 return 1; 612 928 } … … 616 932 function GetAttributeNames() 617 933 { 618 $table = &$this->TableInfo();934 $table = $this->TableInfo(); 619 935 if (!$table) return false; 620 936 return array_keys($table->flds); … … 623 939 }; 624 940 941 function adodb_GetActiveRecordsClass(&$db, $class, $table,$whereOrderBy,$bindarr, $primkeyArr, 942 $extra) 943 { 944 global $_ADODB_ACTIVE_DBS; 945 946 947 $save = $db->SetFetchMode(ADODB_FETCH_NUM); 948 $qry = "select * from ".$table; 949 950 if (!empty($whereOrderBy)) 951 $qry .= ' WHERE '.$whereOrderBy; 952 if(isset($extra['limit'])) 953 { 954 $rows = false; 955 if(isset($extra['offset'])) { 956 $rs = $db->SelectLimit($qry, $extra['limit'], $extra['offset'],$bindarr); 957 } else { 958 $rs = $db->SelectLimit($qry, $extra['limit'],-1,$bindarr); 959 } 960 if ($rs) { 961 while (!$rs->EOF) { 962 $rows[] = $rs->fields; 963 $rs->MoveNext(); 964 } 965 } 966 } else 967 $rows = $db->GetAll($qry,$bindarr); 968 969 $db->SetFetchMode($save); 970 971 $false = false; 972 973 if ($rows === false) { 974 return $false; 975 } 976 977 978 if (!class_exists($class)) { 979 $db->outp_throw("Unknown class $class in GetActiveRecordsClass()",'GetActiveRecordsClass'); 980 return $false; 981 } 982 $arr = array(); 983 // arrRef will be the structure that knows about our objects. 984 // It is an associative array. 985 // We will, however, return arr, preserving regular 0.. order so that 986 // obj[0] can be used by app developpers. 987 $arrRef = array(); 988 $bTos = array(); // Will store belongTo's indices if any 989 foreach($rows as $row) { 990 991 $obj = new $class($table,$primkeyArr,$db); 992 if ($obj->ErrorNo()){ 993 $db->_errorMsg = $obj->ErrorMsg(); 994 return $false; 995 } 996 $obj->Set($row); 997 $arr[] = $obj; 998 } // foreach($rows as $row) 999 1000 return $arr; 1001 } 625 1002 ?>
Note: See TracChangeset
for help on using the changeset viewer.