4 @version V5.14 8 Sept 2011 (c) 2000-2011 John Lim (jlim#natsoft.com). All rights reserved.
5 Latest version is available at http://adodb.sourceforge.net
7 Released under both BSD license and Lesser GPL library license.
8 Whenever there is any discrepancy between the two licenses,
9 the BSD license will take precedence.
11 Active Record implementation. Superset of Zend Framework's.
15 See http://www-128.ibm.com/developerworks/java/library/j-cb03076/?ca=dgr-lnxw01ActiveRecord
16 for info on Ruby on Rails Active Record implementation
20 global $_ADODB_ACTIVE_DBS;
21 global $ADODB_ACTIVE_CACHESECS; // set to true to enable caching of metadata such as field info
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.
25 // array of ADODB_Active_DB's, indexed by ADODB_Active_Record->_dbat
26 $_ADODB_ACTIVE_DBS = array();
27 $ACTIVE_RECORD_SAFETY = true;
28 $ADODB_ACTIVE_DEFVALS = false;
29 $ADODB_ACTIVE_CACHESECS = 0;
31 class ADODB_Active_DB
{
32 var $db; // ADOConnection
33 var $tables; // assoc array of ADODB_Active_Table objects, indexed by tablename
36 class ADODB_Active_Table
{
37 var $name; // table name
38 var $flds; // assoc array of adofieldobjs, indexed by fieldname
39 var $keys; // assoc array of primary keys, indexed by fieldname
40 var $_created; // only used when stored as a cached file
41 var $_belongsTo = array();
42 var $_hasMany = array();
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
48 // returns index into $_ADODB_ACTIVE_DBS
49 function ADODB_SetDatabaseAdapter(&$db, $index=false)
51 global $_ADODB_ACTIVE_DBS;
53 foreach($_ADODB_ACTIVE_DBS as $k => $d) {
54 if (PHP_VERSION
>= 5) {
55 if ($d->db
=== $db) return $k;
57 if ($d->db
->_connectionID
=== $db->_connectionID
&& $db->database
== $d->db
->database
)
62 $obj = new ADODB_Active_DB();
64 $obj->tables
= array();
66 if ($index == false) $index = sizeof($_ADODB_ACTIVE_DBS);
69 $_ADODB_ACTIVE_DBS[$index] = $obj;
71 return sizeof($_ADODB_ACTIVE_DBS)-1;
75 class ADODB_Active_Record
{
76 static $_changeNames = true; // dynamically pluralize table names
77 static $_quoteNames = false;
79 static $_foreignSuffix = '_id'; //
80 var $_dbat; // associative index pointing to ADODB_Active_DB eg. $ADODB_Active_DBS[_dbat]
81 var $_table; // tablename, if set in class definition then use it as table name
82 var $_tableat; // associative index pointing to ADODB_Active_Table, eg $ADODB_Active_DBS[_dbat]->tables[$this->_tableat]
83 var $_where; // where clause set in Load()
84 var $_saved = false; // indicates whether data is already inserted.
85 var $_lasterr = false; // last error message
86 var $_original = false; // the original values loaded or inserted, refreshed on update
88 var $foreignName; // CFR: class name when in a relationship
90 static function UseDefaultValues($bool=null)
92 global $ADODB_ACTIVE_DEFVALS;
93 if (isset($bool)) $ADODB_ACTIVE_DEFVALS = $bool;
94 return $ADODB_ACTIVE_DEFVALS;
98 static function SetDatabaseAdapter(&$db, $index=false)
100 return ADODB_SetDatabaseAdapter($db, $index);
104 public function __set($name, $value)
106 $name = str_replace(' ', '_', $name);
107 $this->$name = $value;
111 function __construct($table = false, $pkeyarr=false, $db=false)
113 global $ADODB_ASSOC_CASE,$_ADODB_ACTIVE_DBS;
115 if ($db == false && is_object($pkeyarr)) {
121 if (!empty($this->_table
)) $table = $this->_table
;
122 else $table = $this->_pluralize(get_class($this));
124 $this->foreignName
= strtolower(get_class($this)); // CFR: default foreign name
126 $this->_dbat
= ADODB_Active_Record
::SetDatabaseAdapter($db);
127 } else if (!isset($this->_dbat
)) {
128 if (sizeof($_ADODB_ACTIVE_DBS) == 0) $this->Error("No database connection set; use ADOdb_Active_Record::SetDatabaseAdapter(\$db)",'ADODB_Active_Record::__constructor');
129 end($_ADODB_ACTIVE_DBS);
130 $this->_dbat
= key($_ADODB_ACTIVE_DBS);
133 $this->_table
= $table;
134 $this->_tableat
= $table; # reserved for setting the assoc value to a non-table name, eg. the sql string in future
136 $this->UpdateActiveTable($pkeyarr);
141 $class = get_class($this);
145 function _pluralize($table)
147 if (!ADODB_Active_Record
::$_changeNames) return $table;
149 $ut = strtoupper($table);
150 $len = strlen($table);
151 $lastc = $ut[$len-1];
152 $lastc2 = substr($ut,$len-2);
157 return substr($table,0,$len-1).'ies';
161 if ($lastc2 == 'CH' ||
$lastc2 == 'SH')
168 // CFR Lamest singular inflector ever - @todo Make it real!
169 // Note: There is an assumption here...and it is that the argument's length >= 4
170 function _singularize($tables)
173 if (!ADODB_Active_Record
::$_changeNames) return $table;
175 $ut = strtoupper($tables);
176 $len = strlen($tables);
177 if($ut[$len-1] != 'S')
178 return $tables; // I know...forget oxen
179 if($ut[$len-2] != 'E')
180 return substr($tables, 0, $len-1);
185 return substr($tables, 0, $len-2);
187 return substr($tables, 0, $len-3) . 'y';
189 if($ut[$len-4] == 'C' ||
$ut[$len-4] == 'S')
190 return substr($tables, 0, $len-2);
192 return substr($tables, 0, $len-1); // ?
196 function hasMany($foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record')
198 $ar = new $foreignClass($foreignRef);
199 $ar->foreignName
= $foreignRef;
200 $ar->UpdateActiveTable();
201 $ar->foreignKey
= ($foreignKey) ?
$foreignKey : $foreignRef.ADODB_Active_Record
::$_foreignSuffix;
202 $table =& $this->TableInfo();
203 $table->_hasMany
[$foreignRef] = $ar;
204 # $this->$foreignRef = $this->_hasMany[$foreignRef]; // WATCHME Removed assignment by ref. to please __get()
207 // use when you don't want ADOdb to auto-pluralize tablename
208 static function TableHasMany($table, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record')
210 $ar = new ADODB_Active_Record($table);
211 $ar->hasMany($foreignRef, $foreignKey, $foreignClass);
214 // use when you don't want ADOdb to auto-pluralize tablename
215 static function TableKeyHasMany($table, $tablePKey, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record')
217 if (!is_array($tablePKey)) $tablePKey = array($tablePKey);
218 $ar = new ADODB_Active_Record($table,$tablePKey);
219 $ar->hasMany($foreignRef, $foreignKey, $foreignClass);
223 // use when you want ADOdb to auto-pluralize tablename for you. Note that the class must already be defined.
224 // e.g. class Person will generate relationship for table Persons
225 static function ClassHasMany($parentclass, $foreignRef, $foreignKey = false, $foreignClass = 'ADODB_Active_Record')
227 $ar = new $parentclass();
228 $ar->hasMany($foreignRef, $foreignKey, $foreignClass);
232 function belongsTo($foreignRef,$foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record')
236 $ar = new $parentClass($this->_pluralize($foreignRef));
237 $ar->foreignName
= $foreignRef;
238 $ar->parentKey
= $parentKey;
239 $ar->UpdateActiveTable();
240 $ar->foreignKey
= ($foreignKey) ?
$foreignKey : $foreignRef.ADODB_Active_Record
::$_foreignSuffix;
242 $table =& $this->TableInfo();
243 $table->_belongsTo
[$foreignRef] = $ar;
244 # $this->$foreignRef = $this->_belongsTo[$foreignRef];
247 static function ClassBelongsTo($class, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record')
250 $ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass);
253 static function TableBelongsTo($table, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record')
255 $ar = new ADOdb_Active_Record($table);
256 $ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass);
259 static function TableKeyBelongsTo($table, $tablePKey, $foreignRef, $foreignKey=false, $parentKey='', $parentClass = 'ADODB_Active_Record')
261 if (!is_array($tablePKey)) $tablePKey = array($tablePKey);
262 $ar = new ADOdb_Active_Record($table, $tablePKey);
263 $ar->belongsTo($foreignRef, $foreignKey, $parentKey, $parentClass);
268 * __get Access properties - used for lazy loading
274 function __get($name)
276 return $this->LoadRelations($name, '', -1, -1);
280 * @param string $name
281 * @param string $whereOrderBy : eg. ' AND field1 = value ORDER BY field2'
286 function LoadRelations($name, $whereOrderBy='', $offset=-1,$limit=-1)
289 $table = $this->TableInfo();
290 if ($limit >= 0) $extras['limit'] = $limit;
291 if ($offset >= 0) $extras['offset'] = $offset;
293 if (strlen($whereOrderBy))
294 if (!preg_match('/^[ \n\r]*AND/i',$whereOrderBy))
295 if (!preg_match('/^[ \n\r]*ORDER[ \n\r]/i',$whereOrderBy))
296 $whereOrderBy = 'AND '.$whereOrderBy;
298 if(!empty($table->_belongsTo
[$name]))
300 $obj = $table->_belongsTo
[$name];
301 $columnName = $obj->foreignKey
;
302 if(empty($this->$columnName))
306 if ($obj->parentKey
) $key = $obj->parentKey
;
307 else $key = reset($table->keys
);
309 $arrayOfOne = $obj->Find($key.'='.$this->$columnName.' '.$whereOrderBy,false,false,$extras);
311 $this->$name = $arrayOfOne[0];
312 return $arrayOfOne[0];
316 if(!empty($table->_hasMany
[$name]))
318 $obj = $table->_hasMany
[$name];
319 $key = reset($table->keys
);
321 if (!is_numeric($id)) {
323 $id = $db->qstr($id);
325 $objs = $obj->Find($obj->foreignKey
.'='.$id. ' '.$whereOrderBy,false,false,$extras);
326 if (!$objs) $objs = array();
327 $this->$name = $objs;
333 //////////////////////////////////
336 function UpdateActiveTable($pkeys=false,$forceUpdate=false)
338 global $ADODB_ASSOC_CASE,$_ADODB_ACTIVE_DBS , $ADODB_CACHE_DIR, $ADODB_ACTIVE_CACHESECS;
339 global $ADODB_ACTIVE_DEFVALS,$ADODB_FETCH_MODE;
341 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat
];
343 $table = $this->_table
;
344 $tables = $activedb->tables
;
345 $tableat = $this->_tableat
;
346 if (!$forceUpdate && !empty($tables[$tableat])) {
348 $acttab = $tables[$tableat];
349 foreach($acttab->flds
as $name => $fld) {
350 if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value
))
351 $this->$name = $fld->default_value
;
358 $fname = $ADODB_CACHE_DIR . '/adodb_' . $db->databaseType
. '_active_'. $table . '.cache';
359 if (!$forceUpdate && $ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR && file_exists($fname)) {
360 $fp = fopen($fname,'r');
361 @flock
($fp, LOCK_SH
);
362 $acttab = unserialize(fread($fp,100000));
364 if ($acttab->_created +
$ADODB_ACTIVE_CACHESECS - (abs(rand()) %
16) > time()) {
365 // abs(rand()) randomizes deletion, reducing contention to delete/refresh file
366 // ideally, you should cache at least 32 secs
368 foreach($acttab->flds
as $name => $fld) {
369 if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value
))
370 $this->$name = $fld->default_value
;
375 $activedb->tables
[$table] = $acttab;
377 //if ($db->debug) ADOConnection::outp("Reading cached active record file: $fname");
379 } else if ($db->debug
) {
380 ADOConnection
::outp("Refreshing cached active record file: $fname");
383 $activetab = new ADODB_Active_Table();
384 $activetab->name
= $table;
386 $save = $ADODB_FETCH_MODE;
387 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC
;
388 if ($db->fetchMode
!== false) $savem = $db->SetFetchMode(false);
390 $cols = $db->MetaColumns($table);
392 if (isset($savem)) $db->SetFetchMode($savem);
393 $ADODB_FETCH_MODE = $save;
396 $this->Error("Invalid table name: $table",'UpdateActiveTable');
401 if (isset($fld->primary_key
)) {
403 foreach($cols as $name => $fld) {
404 if (!empty($fld->primary_key
)) $pkeys[] = $name;
407 $pkeys = $this->GetPrimaryKeys($db, $table);
410 $this->Error("No primary key found for table $table",'UpdateActiveTable');
417 switch($ADODB_ASSOC_CASE) {
419 foreach($cols as $name => $fldobj) {
420 $name = strtolower($name);
421 if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value
))
422 $this->$name = $fldobj->default_value
;
425 $attr[$name] = $fldobj;
427 foreach($pkeys as $k => $name) {
428 $keys[strtolower($name)] = strtolower($name);
433 foreach($cols as $name => $fldobj) {
434 $name = strtoupper($name);
436 if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value
))
437 $this->$name = $fldobj->default_value
;
440 $attr[$name] = $fldobj;
443 foreach($pkeys as $k => $name) {
444 $keys[strtoupper($name)] = strtoupper($name);
448 foreach($cols as $name => $fldobj) {
449 $name = ($fldobj->name
);
451 if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value
))
452 $this->$name = $fldobj->default_value
;
455 $attr[$name] = $fldobj;
457 foreach($pkeys as $k => $name) {
458 $keys[$name] = $cols[$name]->name
;
463 $activetab->keys
= $keys;
464 $activetab->flds
= $attr;
466 if ($ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR) {
467 $activetab->_created
= time();
468 $s = serialize($activetab);
469 if (!function_exists('adodb_write_file')) include(ADODB_DIR
.'/adodb-csvlib.inc.php');
470 adodb_write_file($fname,$s);
472 if (isset($activedb->tables
[$table])) {
473 $oldtab = $activedb->tables
[$table];
475 if ($oldtab) $activetab->_belongsTo
= $oldtab->_belongsTo
;
476 if ($oldtab) $activetab->_hasMany
= $oldtab->_hasMany
;
478 $activedb->tables
[$table] = $activetab;
481 function GetPrimaryKeys(&$db, $table)
483 return $db->MetaPrimaryKeys($table);
486 // error handler for both PHP4+5.
487 function Error($err,$fn)
489 global $_ADODB_ACTIVE_DBS;
491 $fn = get_class($this).'::'.$fn;
492 $this->_lasterr
= $fn.': '.$err;
494 if ($this->_dbat
< 0) $db = false;
496 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat
];
500 if (function_exists('adodb_throw')) {
501 if (!$db) adodb_throw('ADOdb_Active_Record', $fn, -1, $err, 0, 0, false);
502 else adodb_throw($db->databaseType
, $fn, -1, $err, 0, 0, $db);
504 if (!$db ||
$db->debug
) ADOConnection
::outp($this->_lasterr
);
508 // return last error message
511 if (!function_exists('adodb_throw')) {
512 if ($this->_dbat
< 0) $db = false;
513 else $db = $this->DB();
515 // last error could be database error too
516 if ($db && $db->ErrorMsg()) return $db->ErrorMsg();
518 return $this->_lasterr
;
523 if ($this->_dbat
< 0) return -9999; // no database connection...
526 return (int) $db->ErrorNo();
530 // retrieve ADOConnection from _ADODB_Active_DBs
533 global $_ADODB_ACTIVE_DBS;
535 if ($this->_dbat
< 0) {
537 $this->Error("No database connection set: use ADOdb_Active_Record::SetDatabaseAdaptor(\$db)", "DB");
540 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat
];
545 // retrieve ADODB_Active_Table
546 function &TableInfo()
548 global $_ADODB_ACTIVE_DBS;
549 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat
];
550 $table = $activedb->tables
[$this->_tableat
];
555 // I have an ON INSERT trigger on a table that sets other columns in the table.
556 // So, I find that for myTable, I want to reload an active record after saving it. -- Malcolm Cook
559 $db =& $this->DB(); if (!$db) return false;
560 $table =& $this->TableInfo();
561 $where = $this->GenWhere($db, $table);
562 return($this->Load($where));
566 // set a numeric array (using natural table field ordering) as object properties
569 global $ACTIVE_RECORD_SAFETY;
574 $this->_saved
= false;
578 $this->_saved
= true;
580 $table = $this->TableInfo();
581 if ($ACTIVE_RECORD_SAFETY && sizeof($table->flds
) != sizeof($row)) {
584 if (sizeof($row) == 2 * sizeof($table->flds
)) {
585 // Only keep string keys
586 $keys = array_filter(array_keys($row), 'is_string');
587 if (sizeof($keys) == sizeof($table->flds
))
591 $this->Error("Table structure of $this->_table has changed","Load");
597 $keys = array_keys($row);
601 $this->_original
= array();
602 foreach($table->flds
as $name=>$fld) {
603 $value = $row[current($keys)];
604 $this->$name = $value;
605 $this->_original
[] = $value;
613 // get last inserted id for INSERT
614 function LastInsertID(&$db,$fieldname)
616 if ($db->hasInsertID
)
617 $val = $db->Insert_ID($this->_table
,$fieldname);
621 if (is_null($val) ||
$val === false) {
622 // this might not work reliably in multi-user environment
623 return $db->GetOne("select max(".$fieldname.") from ".$this->_table
);
628 // quote data in where clause
629 function doquote(&$db, $val,$t)
633 if (strpos($db->databaseType
,'postgres') !== false) return $db->qstr($val);
636 if (empty($val)) return 'null';
642 if (is_null($val)) return 'null';
644 if (strlen($val)>1 &&
645 (strncmp($val,"'",1) != 0 ||
substr($val,strlen($val)-1,1) != "'")) {
646 return $db->qstr($val);
655 // generate where clause for an UPDATE/SELECT
656 function GenWhere(&$db, &$table)
658 $keys = $table->keys
;
661 foreach($keys as $k) {
662 $f = $table->flds
[$k];
664 $parr[] = $k.' = '.$this->doquote($db,$this->$k,$db->MetaType($f->type
));
667 return implode(' and ', $parr);
671 function _QName($n,$db=false)
673 if (!ADODB_Active_Record
::$_quoteNames) return $n;
674 if (!$db) $db = $this->DB(); if (!$db) return false;
675 return $db->nameQuote
.$n.$db->nameQuote
;
678 //------------------------------------------------------------ Public functions below
680 function Load($where=null,$bindarr=false)
682 global $ADODB_FETCH_MODE;
684 $db = $this->DB(); if (!$db) return false;
685 $this->_where
= $where;
687 $save = $ADODB_FETCH_MODE;
688 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
689 if ($db->fetchMode
!== false) $savem = $db->SetFetchMode(false);
691 $qry = "select * from ".$this->_table
;
694 $qry .= ' WHERE '.$where;
696 $row = $db->GetRow($qry,$bindarr);
698 if (isset($savem)) $db->SetFetchMode($savem);
699 $ADODB_FETCH_MODE = $save;
701 return $this->Set($row);
704 # useful for multiple record inserts
705 # see http://phplens.com/lens/lensforum/msgs.php?id=17795
709 $this->_saved
= false;
710 $this->_lasterr
= false;
711 $this->_original
= false;
712 $vars=get_object_vars($this);
713 foreach($vars as $k=>$v){
714 if(substr($k,0,1)!=='_'){
718 $this->foreignName
=strtolower(get_class($this));
725 if ($this->_saved
) $ok = $this->Update();
726 else $ok = $this->Insert();
735 $db = $this->DB(); if (!$db) return false;
737 $table = $this->TableInfo();
743 foreach($table->flds
as $name=>$fld) {
745 if(!is_array($val) ||
!is_null($val) ||
!array_key_exists($name, $table->keys
)) {
747 $names[] = $this->_QName($name,$db);
748 $valstr[] = $db->Param($cnt);
754 foreach($table->flds
as $name=>$fld) {
757 $valstr[] = $db->Param($cnt);
761 $sql = 'INSERT INTO '.$this->_table
."(".implode(',',$names).') VALUES ('.implode(',',$valstr).')';
762 $ok = $db->Execute($sql,$valarr);
765 $this->_saved
= true;
767 foreach($table->keys
as $k) {
768 if (is_null($this->$k)) {
773 if ($autoinc && sizeof($table->keys
) == 1) {
774 $k = reset($table->keys
);
775 $this->$k = $this->LastInsertID($db,$k);
779 $this->_original
= $valarr;
785 $db = $this->DB(); if (!$db) return false;
786 $table = $this->TableInfo();
788 $where = $this->GenWhere($db,$table);
789 $sql = 'DELETE FROM '.$this->_table
.' WHERE '.$where;
790 $ok = $db->Execute($sql);
792 return $ok ?
true : false;
795 // returns an array of active record objects
796 function Find($whereOrderBy,$bindarr=false,$pkeysArr=false,$extra=array())
798 $db = $this->DB(); if (!$db ||
empty($this->_table
)) return false;
799 $arr = $db->GetActiveRecordsClass(get_class($this),$this->_table
, $whereOrderBy,$bindarr,$pkeysArr,$extra);
803 // returns 0 on error, 1 on update, 2 on insert
806 global $ADODB_ASSOC_CASE;
808 $db = $this->DB(); if (!$db) return false;
809 $table = $this->TableInfo();
811 $pkey = $table->keys
;
813 foreach($table->flds
as $name=>$fld) {
817 if (isset($fld->not_null) && $fld->not_null) {
818 if (isset($fld->default_value) && strlen($fld->default_value)) continue;
820 $this->Error("Cannot update null into $name","Replace");
825 if (is_null($val) && !empty($fld->auto_increment
)) {
829 if (is_array($val)) continue;
831 $t = $db->MetaType($fld->type
);
832 $arr[$name] = $this->doquote($db,$val,$t);
836 if (!is_array($pkey)) $pkey = array($pkey);
839 if ($ADODB_ASSOC_CASE == 0)
840 foreach($pkey as $k => $v)
841 $pkey[$k] = strtolower($v);
842 elseif ($ADODB_ASSOC_CASE == 1)
843 foreach($pkey as $k => $v)
844 $pkey[$k] = strtoupper($v);
846 $ok = $db->Replace($this->_table
,$arr,$pkey);
848 $this->_saved
= true; // 1= update 2=insert
851 foreach($table->keys
as $k) {
852 if (is_null($this->$k)) {
857 if ($autoinc && sizeof($table->keys
) == 1) {
858 $k = reset($table->keys
);
859 $this->$k = $this->LastInsertID($db,$k);
863 $this->_original
= $valarr;
868 // returns 0 on error, 1 on update, -1 if no change in data (no update)
871 $db = $this->DB(); if (!$db) return false;
872 $table = $this->TableInfo();
874 $where = $this->GenWhere($db, $table);
877 $this->error("Where missing for table $table", "Update");
885 foreach($table->flds
as $name=>$fld) {
890 if (isset($table->keys
[$name]) ||
is_array($val))
894 if (isset($fld->not_null
) && $fld->not_null
) {
895 if (isset($fld->default_value
) && strlen($fld->default_value
)) continue;
897 $this->Error("Cannot set field $name to NULL","Update");
903 if (isset($this->_original
[$i]) && strcmp($val,$this->_original
[$i]) == 0) {
907 $pairs[] = $this->_QName($name,$db).'='.$db->Param($cnt);
912 if (!$cnt) return -1;
913 $sql = 'UPDATE '.$this->_table
." SET ".implode(",",$pairs)." WHERE ".$where;
914 $ok = $db->Execute($sql,$valarr);
916 $this->_original
= $neworig;
922 function GetAttributeNames()
924 $table = $this->TableInfo();
925 if (!$table) return false;
926 return array_keys($table->flds
);
931 function adodb_GetActiveRecordsClass(&$db, $class, $table,$whereOrderBy,$bindarr, $primkeyArr,
934 global $_ADODB_ACTIVE_DBS;
937 $save = $db->SetFetchMode(ADODB_FETCH_NUM
);
938 $qry = "select * from ".$table;
940 if (!empty($whereOrderBy))
941 $qry .= ' WHERE '.$whereOrderBy;
942 if(isset($extra['limit']))
945 if(isset($extra['offset'])) {
946 $rs = $db->SelectLimit($qry, $extra['limit'], $extra['offset'],$bindarr);
948 $rs = $db->SelectLimit($qry, $extra['limit'],-1,$bindarr);
952 $rows[] = $rs->fields
;
957 $rows = $db->GetAll($qry,$bindarr);
959 $db->SetFetchMode($save);
963 if ($rows === false) {
968 if (!class_exists($class)) {
969 $db->outp_throw("Unknown class $class in GetActiveRecordsClass()",'GetActiveRecordsClass');
973 // arrRef will be the structure that knows about our objects.
974 // It is an associative array.
975 // We will, however, return arr, preserving regular 0.. order so that
976 // obj[0] can be used by app developpers.
978 $bTos = array(); // Will store belongTo's indices if any
979 foreach($rows as $row) {
981 $obj = new $class($table,$primkeyArr,$db);
982 if ($obj->ErrorNo()){
983 $db->_errorMsg
= $obj->ErrorMsg();
988 } // foreach($rows as $row)