4 @version V5.06 29 Sept 2008 (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.
13 This is "Active Record eXtended" to support JOIN, WORK and LAZY mode by Chris Ravenscroft chris#voilaweb.com
17 See http://www-128.ibm.com/developerworks/java/library/j-cb03076/?ca=dgr-lnxw01ActiveRecord
18 for info on Ruby on Rails Active Record implementation
22 // CFR: Active Records Definitions
23 define('ADODB_JOIN_AR', 0x01);
24 define('ADODB_WORK_AR', 0x02);
25 define('ADODB_LAZY_AR', 0x03);
28 global $_ADODB_ACTIVE_DBS;
29 global $ADODB_ACTIVE_CACHESECS; // set to true to enable caching of metadata such as field info
30 global $ACTIVE_RECORD_SAFETY; // set to false to disable safety checks
31 global $ADODB_ACTIVE_DEFVALS; // use default values of table definition when creating new active record.
33 // array of ADODB_Active_DB's, indexed by ADODB_Active_Record->_dbat
34 $_ADODB_ACTIVE_DBS = array();
35 $ACTIVE_RECORD_SAFETY = true; // CFR: disabled while playing with relations
36 $ADODB_ACTIVE_DEFVALS = false;
38 class ADODB_Active_DB
{
39 var $db; // ADOConnection
40 var $tables; // assoc array of ADODB_Active_Table objects, indexed by tablename
43 class ADODB_Active_Table
{
44 var $name; // table name
45 var $flds; // assoc array of adofieldobjs, indexed by fieldname
46 var $keys; // assoc array of primary keys, indexed by fieldname
47 var $_created; // only used when stored as a cached file
48 var $_belongsTo = array();
49 var $_hasMany = array();
50 var $_colsCount; // total columns count, including relations
52 function updateColsCount()
54 $this->_colsCount
= sizeof($this->flds
);
55 foreach($this->_belongsTo
as $foreignTable)
56 $this->_colsCount +
= sizeof($foreignTable->TableInfo()->flds
);
57 foreach($this->_hasMany
as $foreignTable)
58 $this->_colsCount +
= sizeof($foreignTable->TableInfo()->flds
);
62 // returns index into $_ADODB_ACTIVE_DBS
63 function ADODB_SetDatabaseAdapter(&$db)
65 global $_ADODB_ACTIVE_DBS;
67 foreach($_ADODB_ACTIVE_DBS as $k => $d) {
68 if (PHP_VERSION
>= 5) {
69 if ($d->db
=== $db) return $k;
71 if ($d->db
->_connectionID
=== $db->_connectionID
&& $db->database
== $d->db
->database
)
76 $obj = new ADODB_Active_DB();
78 $obj->tables
= array();
80 $_ADODB_ACTIVE_DBS[] = $obj;
82 return sizeof($_ADODB_ACTIVE_DBS)-1;
86 class ADODB_Active_Record
{
87 static $_changeNames = true; // dynamically pluralize table names
88 static $_foreignSuffix = '_id'; //
89 var $_dbat; // associative index pointing to ADODB_Active_DB eg. $ADODB_Active_DBS[_dbat]
90 var $_table; // tablename, if set in class definition then use it as table name
91 var $_sTable; // singularized table name
92 var $_pTable; // pluralized table name
93 var $_tableat; // associative index pointing to ADODB_Active_Table, eg $ADODB_Active_DBS[_dbat]->tables[$this->_tableat]
94 var $_where; // where clause set in Load()
95 var $_saved = false; // indicates whether data is already inserted.
96 var $_lasterr = false; // last error message
97 var $_original = false; // the original values loaded or inserted, refreshed on update
99 var $foreignName; // CFR: class name when in a relationship
101 static function UseDefaultValues($bool=null)
103 global $ADODB_ACTIVE_DEFVALS;
104 if (isset($bool)) $ADODB_ACTIVE_DEFVALS = $bool;
105 return $ADODB_ACTIVE_DEFVALS;
109 static function SetDatabaseAdapter(&$db)
111 return ADODB_SetDatabaseAdapter($db);
115 public function __set($name, $value)
117 $name = str_replace(' ', '_', $name);
118 $this->$name = $value;
122 // Note: if $table is defined, then we will use it as our table name
123 // Otherwise we will use our classname...
124 // In our database, table names are pluralized (because there can be
125 // more than one row!)
126 // Similarly, if $table is defined here, it has to be plural form.
128 // $options is an array that allows us to tweak the constructor's behaviour
129 // if $options['refresh'] is true, we re-scan our metadata information
130 // if $options['new'] is true, we forget all relations
131 function __construct($table = false, $pkeyarr=false, $db=false, $options=array())
133 global $ADODB_ASSOC_CASE,$_ADODB_ACTIVE_DBS;
135 if ($db == false && is_object($pkeyarr)) {
142 // table argument exists. It is expected to be
143 // already plural form.
144 $this->_pTable
= $table;
145 $this->_sTable
= $this->_singularize($this->_pTable
);
149 // We will use current classname as table name.
150 // We need to pluralize it for the real table name.
151 $this->_sTable
= strtolower(get_class($this));
152 $this->_pTable
= $this->_pluralize($this->_sTable
);
154 $this->_table
= &$this->_pTable
;
156 $this->foreignName
= $this->_sTable
; // CFR: default foreign name (singular)
159 $this->_dbat
= ADODB_Active_Record
::SetDatabaseAdapter($db);
161 $this->_dbat
= sizeof($_ADODB_ACTIVE_DBS)-1;
164 if ($this->_dbat
< 0) $this->Error("No database connection set; use ADOdb_Active_Record::SetDatabaseAdapter(\$db)",'ADODB_Active_Record::__constructor');
166 $this->_tableat
= $this->_table
; # reserved for setting the assoc value to a non-table name, eg. the sql string in future
168 // CFR: Just added this option because UpdateActiveTable() can refresh its information
169 // but there was no way to ask it to do that.
170 $forceUpdate = (isset($options['refresh']) && true === $options['refresh']);
171 $this->UpdateActiveTable($pkeyarr, $forceUpdate);
172 if(isset($options['new']) && true === $options['new'])
174 $table =& $this->TableInfo();
175 unset($table->_hasMany
);
176 unset($table->_belongsTo
);
177 $table->_hasMany
= array();
178 $table->_belongsTo
= array();
184 $class = get_class($this);
188 // CFR: Constants found in Rails
189 static $IrregularP = array(
190 'PERSON' => 'people',
193 'CHILD' => 'children',
197 static $IrregularS = array(
198 'PEOPLE' => 'PERSON',
201 'CHILDREN' => 'child',
205 static $WeIsI = array(
207 'INFORMATION' => true,
216 function _pluralize($table)
218 if (!ADODB_Active_Record
::$_changeNames) return $table;
220 $ut = strtoupper($table);
221 if(isset(self
::$WeIsI[$ut]))
225 if(isset(self
::$IrregularP[$ut]))
227 return self
::$IrregularP[$ut];
229 $len = strlen($table);
230 $lastc = $ut[$len-1];
231 $lastc2 = substr($ut,$len-2);
236 return substr($table,0,$len-1).'ies';
240 if ($lastc2 == 'CH' ||
$lastc2 == 'SH')
247 // CFR Lamest singular inflector ever - @todo Make it real!
248 // Note: There is an assumption here...and it is that the argument's length >= 4
249 function _singularize($table)
252 if (!ADODB_Active_Record
::$_changeNames) return $table;
254 $ut = strtoupper($table);
255 if(isset(self
::$WeIsI[$ut]))
259 if(isset(self
::$IrregularS[$ut]))
261 return self
::$IrregularS[$ut];
263 $len = strlen($table);
264 if($ut[$len-1] != 'S')
265 return $table; // I know...forget oxen
266 if($ut[$len-2] != 'E')
267 return substr($table, 0, $len-1);
272 return substr($table, 0, $len-2);
274 return substr($table, 0, $len-3) . 'y';
276 if($ut[$len-4] == 'C' ||
$ut[$len-4] == 'S')
277 return substr($table, 0, $len-2);
279 return substr($table, 0, $len-1); // ?
284 * ar->foreignName will contain the name of the tables associated with this table because
285 * these other tables' rows may also be referenced by this table using theirname_id or the provided
286 * foreign keys (this index name is stored in ar->foreignKey)
288 * this-table.id = other-table-#1.this-table_id
289 * = other-table-#2.this-table_id
291 function hasMany($foreignRef,$foreignKey=false)
293 $ar = new ADODB_Active_Record($foreignRef);
294 $ar->foreignName
= $foreignRef;
295 $ar->UpdateActiveTable();
296 $ar->foreignKey
= ($foreignKey) ?
$foreignKey : strtolower(get_class($this)) . self
::$_foreignSuffix;
298 $table =& $this->TableInfo();
299 if(!isset($table->_hasMany
[$foreignRef]))
301 $table->_hasMany
[$foreignRef] = $ar;
302 $table->updateColsCount();
304 # @todo Can I make this guy be lazy?
305 $this->$foreignRef = $table->_hasMany
[$foreignRef]; // WATCHME Removed assignment by ref. to please __get()
309 * ar->foreignName will contain the name of the tables associated with this table because
310 * this table's rows may also be referenced by those tables using thistable_id or the provided
311 * foreign keys (this index name is stored in ar->foreignKey)
313 * this-table.other-table_id = other-table.id
315 function belongsTo($foreignRef,$foreignKey=false)
319 $ar = new ADODB_Active_Record($this->_pluralize($foreignRef));
320 $ar->foreignName
= $foreignRef;
321 $ar->UpdateActiveTable();
322 $ar->foreignKey
= ($foreignKey) ?
$foreignKey : $ar->foreignName
. self
::$_foreignSuffix;
324 $table =& $this->TableInfo();
325 if(!isset($table->_belongsTo
[$foreignRef]))
327 $table->_belongsTo
[$foreignRef] = $ar;
328 $table->updateColsCount();
330 $this->$foreignRef = $table->_belongsTo
[$foreignRef];
334 * __get Access properties - used for lazy loading
340 function __get($name)
342 return $this->LoadRelations($name, '', -1. -1);
345 function LoadRelations($name, $whereOrderBy, $offset=-1, $limit=-1)
348 if($offset >= 0) $extras['offset'] = $offset;
349 if($limit >= 0) $extras['limit'] = $limit;
350 $table =& $this->TableInfo();
352 if (strlen($whereOrderBy))
353 if (!preg_match('/^[ \n\r]*AND/i',$whereOrderBy))
354 if (!preg_match('/^[ \n\r]*ORDER[ \n\r]/i',$whereOrderBy))
355 $whereOrderBy = 'AND '.$whereOrderBy;
357 if(!empty($table->_belongsTo
[$name]))
359 $obj = $table->_belongsTo
[$name];
360 $columnName = $obj->foreignKey
;
361 if(empty($this->$columnName))
365 if(($k = reset($obj->TableInfo()->keys
)))
372 $belongsToId.'='.$this->$columnName.' '.$whereOrderBy, false, false, $extras);
373 $this->$name = $arrayOfOne[0];
377 if(!empty($table->_hasMany
[$name]))
379 $obj = $table->_hasMany
[$name];
380 if(($k = reset($table->keys
)))
387 $obj->foreignKey
.'='.$this->$hasManyId.' '.$whereOrderBy, false, false, $extras);
391 //////////////////////////////////
394 function UpdateActiveTable($pkeys=false,$forceUpdate=false)
396 global $ADODB_ASSOC_CASE,$_ADODB_ACTIVE_DBS , $ADODB_CACHE_DIR, $ADODB_ACTIVE_CACHESECS;
397 global $ADODB_ACTIVE_DEFVALS, $ADODB_FETCH_MODE;
399 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat
];
401 $table = $this->_table
;
402 $tables = $activedb->tables
;
403 $tableat = $this->_tableat
;
404 if (!$forceUpdate && !empty($tables[$tableat])) {
406 $tobj = $tables[$tableat];
407 foreach($tobj->flds
as $name => $fld) {
408 if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value
))
409 $this->$name = $fld->default_value
;
417 $fname = $ADODB_CACHE_DIR . '/adodb_' . $db->databaseType
. '_active_'. $table . '.cache';
418 if (!$forceUpdate && $ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR && file_exists($fname)) {
419 $fp = fopen($fname,'r');
420 @flock
($fp, LOCK_SH
);
421 $acttab = unserialize(fread($fp,100000));
423 if ($acttab->_created +
$ADODB_ACTIVE_CACHESECS - (abs(rand()) %
16) > time()) {
424 // abs(rand()) randomizes deletion, reducing contention to delete/refresh file
425 // ideally, you should cache at least 32 secs
426 $activedb->tables
[$table] = $acttab;
428 //if ($db->debug) ADOConnection::outp("Reading cached active record file: $fname");
430 } else if ($db->debug
) {
431 ADOConnection
::outp("Refreshing cached active record file: $fname");
434 $activetab = new ADODB_Active_Table();
435 $activetab->name
= $table;
437 $save = $ADODB_FETCH_MODE;
438 $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC
;
439 if ($db->fetchMode
!== false) $savem = $db->SetFetchMode(false);
441 $cols = $db->MetaColumns($table);
443 if (isset($savem)) $db->SetFetchMode($savem);
444 $ADODB_FETCH_MODE = $save;
447 $this->Error("Invalid table name: $table",'UpdateActiveTable');
452 if (isset($fld->primary_key
)) {
454 foreach($cols as $name => $fld) {
455 if (!empty($fld->primary_key
)) $pkeys[] = $name;
458 $pkeys = $this->GetPrimaryKeys($db, $table);
461 $this->Error("No primary key found for table $table",'UpdateActiveTable');
468 switch($ADODB_ASSOC_CASE) {
470 foreach($cols as $name => $fldobj) {
471 $name = strtolower($name);
472 if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value
))
473 $this->$name = $fldobj->default_value
;
476 $attr[$name] = $fldobj;
478 foreach($pkeys as $k => $name) {
479 $keys[strtolower($name)] = strtolower($name);
484 foreach($cols as $name => $fldobj) {
485 $name = strtoupper($name);
487 if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value
))
488 $this->$name = $fldobj->default_value
;
491 $attr[$name] = $fldobj;
494 foreach($pkeys as $k => $name) {
495 $keys[strtoupper($name)] = strtoupper($name);
499 foreach($cols as $name => $fldobj) {
500 $name = ($fldobj->name
);
502 if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value
))
503 $this->$name = $fldobj->default_value
;
506 $attr[$name] = $fldobj;
508 foreach($pkeys as $k => $name) {
509 $keys[$name] = $cols[$name]->name
;
514 $activetab->keys
= $keys;
515 $activetab->flds
= $attr;
516 $activetab->updateColsCount();
518 if ($ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR) {
519 $activetab->_created
= time();
520 $s = serialize($activetab);
521 if (!function_exists('adodb_write_file')) include(ADODB_DIR
.'/adodb-csvlib.inc.php');
522 adodb_write_file($fname,$s);
524 if (isset($activedb->tables
[$table])) {
525 $oldtab = $activedb->tables
[$table];
527 if ($oldtab) $activetab->_belongsTo
= $oldtab->_belongsTo
;
528 if ($oldtab) $activetab->_hasMany
= $oldtab->_hasMany
;
530 $activedb->tables
[$table] = $activetab;
533 function GetPrimaryKeys(&$db, $table)
535 return $db->MetaPrimaryKeys($table);
538 // error handler for both PHP4+5.
539 function Error($err,$fn)
541 global $_ADODB_ACTIVE_DBS;
543 $fn = get_class($this).'::'.$fn;
544 $this->_lasterr
= $fn.': '.$err;
546 if ($this->_dbat
< 0) $db = false;
548 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat
];
552 if (function_exists('adodb_throw')) {
553 if (!$db) adodb_throw('ADOdb_Active_Record', $fn, -1, $err, 0, 0, false);
554 else adodb_throw($db->databaseType
, $fn, -1, $err, 0, 0, $db);
556 if (!$db ||
$db->debug
) ADOConnection
::outp($this->_lasterr
);
560 // return last error message
563 if (!function_exists('adodb_throw')) {
564 if ($this->_dbat
< 0) $db = false;
565 else $db = $this->DB();
567 // last error could be database error too
568 if ($db && $db->ErrorMsg()) return $db->ErrorMsg();
570 return $this->_lasterr
;
575 if ($this->_dbat
< 0) return -9999; // no database connection...
578 return (int) $db->ErrorNo();
582 // retrieve ADOConnection from _ADODB_Active_DBs
585 global $_ADODB_ACTIVE_DBS;
587 if ($this->_dbat
< 0) {
589 $this->Error("No database connection set: use ADOdb_Active_Record::SetDatabaseAdaptor(\$db)", "DB");
592 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat
];
597 // retrieve ADODB_Active_Table
598 function &TableInfo()
600 global $_ADODB_ACTIVE_DBS;
602 $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat
];
603 $table = $activedb->tables
[$this->_tableat
];
608 // I have an ON INSERT trigger on a table that sets other columns in the table.
609 // So, I find that for myTable, I want to reload an active record after saving it. -- Malcolm Cook
612 $db =& $this->DB(); if (!$db) return false;
613 $table =& $this->TableInfo();
614 $where = $this->GenWhere($db, $table);
615 return($this->Load($where));
619 // set a numeric array (using natural table field ordering) as object properties
622 global $ACTIVE_RECORD_SAFETY;
627 $this->_saved
= false;
631 $this->_saved
= true;
633 $table = $this->TableInfo();
634 $sizeofFlds = sizeof($table->flds
);
635 $sizeofRow = sizeof($row);
636 if ($ACTIVE_RECORD_SAFETY && $table->_colsCount
!= $sizeofRow && $sizeofFlds != $sizeofRow) {
639 if($sizeofRow == 2 * $table->_colsCount ||
$sizeofRow == 2 * $sizeofFlds) {
640 // Only keep string keys
641 $keys = array_filter(array_keys($row), 'is_string');
642 if (sizeof($keys) == sizeof($table->flds
))
646 $this->Error("Table structure of $this->_table has changed","Load");
652 $keys = array_keys($row);
655 $this->_original
= array();
656 foreach($table->flds
as $name=>$fld)
658 $value = $row[current($keys)];
659 $this->$name = $value;
660 $this->_original
[] = $value;
661 if(!next($keys)) break;
663 $table =& $this->TableInfo();
664 foreach($table->_belongsTo
as $foreignTable)
666 $ft = $foreignTable->TableInfo();
667 $propertyName = $ft->name
;
668 foreach($ft->flds
as $name=>$fld)
670 $value = $row[current($keys)];
671 $foreignTable->$name = $value;
672 $foreignTable->_original
[] = $value;
673 if(!next($keys)) break;
676 foreach($table->_hasMany
as $foreignTable)
678 $ft = $foreignTable->TableInfo();
679 foreach($ft->flds
as $name=>$fld)
681 $value = $row[current($keys)];
682 $foreignTable->$name = $value;
683 $foreignTable->_original
[] = $value;
684 if(!next($keys)) break;
691 // get last inserted id for INSERT
692 function LastInsertID(&$db,$fieldname)
694 if ($db->hasInsertID
)
695 $val = $db->Insert_ID($this->_table
,$fieldname);
699 if (is_null($val) ||
$val === false) {
700 // this might not work reliably in multi-user environment
701 return $db->GetOne("select max(".$fieldname.") from ".$this->_table
);
706 // quote data in where clause
707 function doquote(&$db, $val,$t)
712 if (empty($val)) return 'null';
716 if (is_null($val)) return 'null';
718 if (strlen($val)>1 &&
719 (strncmp($val,"'",1) != 0 ||
substr($val,strlen($val)-1,1) != "'")) {
720 return $db->qstr($val);
729 // generate where clause for an UPDATE/SELECT
730 function GenWhere(&$db, &$table)
732 $keys = $table->keys
;
735 foreach($keys as $k) {
736 $f = $table->flds
[$k];
738 $parr[] = $k.' = '.$this->doquote($db,$this->$k,$db->MetaType($f->type
));
741 return implode(' and ', $parr);
745 //------------------------------------------------------------ Public functions below
747 function Load($where=null,$bindarr=false)
749 $db = $this->DB(); if (!$db) return false;
750 $this->_where
= $where;
752 $save = $db->SetFetchMode(ADODB_FETCH_NUM
);
753 $qry = "select * from ".$this->_table
;
754 $table =& $this->TableInfo();
756 if(($k = reset($table->keys
)))
761 foreach($table->_belongsTo
as $foreignTable)
763 if(($k = reset($foreignTable->TableInfo()->keys
)))
771 $qry .= ' LEFT JOIN '.$foreignTable->_table
.' ON '.
772 $this->_table
.'.'.$foreignTable->foreignKey
.'='.
773 $foreignTable->_table
.'.'.$belongsToId;
775 foreach($table->_hasMany
as $foreignTable)
777 $qry .= ' LEFT JOIN '.$foreignTable->_table
.' ON '.
778 $this->_table
.'.'.$hasManyId.'='.
779 $foreignTable->_table
.'.'.$foreignTable->foreignKey
;
782 $qry .= ' WHERE '.$where;
784 // Simple case: no relations. Load row and return.
785 if((count($table->_hasMany
) +
count($table->_belongsTo
)) < 1)
787 $row = $db->GetRow($qry,$bindarr);
790 $db->SetFetchMode($save);
791 return $this->Set($row);
794 // More complex case when relations have to be collated
795 $rows = $db->GetAll($qry,$bindarr);
798 $db->SetFetchMode($save);
801 $class = get_class($this);
804 if(($k = reset($this->TableInfo()->keys
)))
808 $index = 0; $found = false;
809 /** @todo Improve by storing once and for all in table metadata */
810 /** @todo Also re-use info for hasManyId */
811 foreach($this->TableInfo()->flds
as $fld)
813 if($fld->name
== $myId)
821 $this->outp_throw("Unable to locate key $myId for $class in Load()",'Load');
823 foreach($rows as $row)
825 $rowId = intval($row[$index]);
831 if(!$this->Set($row))
834 $obj = new $class($table,false,$db);
836 // TODO Copy/paste code below: bad!
837 if(count($table->_hasMany
) > 0)
839 foreach($table->_hasMany
as $foreignTable)
841 $foreignName = $foreignTable->foreignName
;
842 if(!empty($obj->$foreignName))
844 if(!is_array($this->$foreignName))
846 $foreignObj = $this->$foreignName;
847 $this->$foreignName = array(clone($foreignObj));
851 $foreignObj = $obj->$foreignName;
852 array_push($this->$foreignName, clone($foreignObj));
857 if(count($table->_belongsTo
) > 0)
859 foreach($table->_belongsTo
as $foreignTable)
861 $foreignName = $foreignTable->foreignName
;
862 if(!empty($obj->$foreignName))
864 if(!is_array($this->$foreignName))
866 $foreignObj = $this->$foreignName;
867 $this->$foreignName = array(clone($foreignObj));
871 $foreignObj = $obj->$foreignName;
872 array_push($this->$foreignName, clone($foreignObj));
885 if ($this->_saved
) $ok = $this->Update();
886 else $ok = $this->Insert();
891 // CFR: Sometimes we may wish to consider that an object is not to be replaced but inserted.
892 // Sample use case: an 'undo' command object (after a delete())
895 $this->_saved
= false;
901 $db = $this->DB(); if (!$db) return false;
903 $table = $this->TableInfo();
909 foreach($table->flds
as $name=>$fld) {
911 if(!is_null($val) ||
!array_key_exists($name, $table->keys
)) {
914 $valstr[] = $db->Param($cnt);
920 foreach($table->flds
as $name=>$fld) {
923 $valstr[] = $db->Param($cnt);
927 $sql = 'INSERT INTO '.$this->_table
."(".implode(',',$names).') VALUES ('.implode(',',$valstr).')';
928 $ok = $db->Execute($sql,$valarr);
931 $this->_saved
= true;
933 foreach($table->keys
as $k) {
934 if (is_null($this->$k)) {
939 if ($autoinc && sizeof($table->keys
) == 1) {
940 $k = reset($table->keys
);
941 $this->$k = $this->LastInsertID($db,$k);
945 $this->_original
= $valarr;
951 $db = $this->DB(); if (!$db) return false;
952 $table = $this->TableInfo();
954 $where = $this->GenWhere($db,$table);
955 $sql = 'DELETE FROM '.$this->_table
.' WHERE '.$where;
956 $ok = $db->Execute($sql);
958 return $ok ?
true : false;
961 // returns an array of active record objects
962 function Find($whereOrderBy,$bindarr=false,$pkeysArr=false,$extra=array())
964 $db = $this->DB(); if (!$db ||
empty($this->_table
)) return false;
965 $table =& $this->TableInfo();
966 $arr = $db->GetActiveRecordsClass(get_class($this),$this, $whereOrderBy,$bindarr,$pkeysArr,$extra,
967 array('foreignName'=>$this->foreignName
, 'belongsTo'=>$table->_belongsTo
, 'hasMany'=>$table->_hasMany
));
971 // CFR: In introduced this method to ensure that inner workings are not disturbed by
972 // subclasses...for instance when GetActiveRecordsClass invokes Find()
973 // Why am I not invoking parent::Find?
974 // Shockingly because I want to preserve PHP4 compatibility.
975 function packageFind($whereOrderBy,$bindarr=false,$pkeysArr=false,$extra=array())
977 $db = $this->DB(); if (!$db ||
empty($this->_table
)) return false;
978 $table =& $this->TableInfo();
979 $arr = $db->GetActiveRecordsClass(get_class($this),$this, $whereOrderBy,$bindarr,$pkeysArr,$extra,
980 array('foreignName'=>$this->foreignName
, 'belongsTo'=>$table->_belongsTo
, 'hasMany'=>$table->_hasMany
));
984 // returns 0 on error, 1 on update, 2 on insert
987 global $ADODB_ASSOC_CASE;
989 $db = $this->DB(); if (!$db) return false;
990 $table = $this->TableInfo();
992 $pkey = $table->keys
;
994 foreach($table->flds
as $name=>$fld) {
998 if (isset($fld->not_null) && $fld->not_null) {
999 if (isset($fld->default_value) && strlen($fld->default_value)) continue;
1001 $this->Error("Cannot update null into $name","Replace");
1006 if (is_null($val) && !empty($fld->auto_increment
)) {
1009 $t = $db->MetaType($fld->type
);
1010 $arr[$name] = $this->doquote($db,$val,$t);
1014 if (!is_array($pkey)) $pkey = array($pkey);
1017 if ($ADODB_ASSOC_CASE == 0)
1018 foreach($pkey as $k => $v)
1019 $pkey[$k] = strtolower($v);
1020 elseif ($ADODB_ASSOC_CASE == 1)
1021 foreach($pkey as $k => $v)
1022 $pkey[$k] = strtoupper($v);
1024 $ok = $db->Replace($this->_table
,$arr,$pkey);
1026 $this->_saved
= true; // 1= update 2=insert
1029 foreach($table->keys
as $k) {
1030 if (is_null($this->$k)) {
1035 if ($autoinc && sizeof($table->keys
) == 1) {
1036 $k = reset($table->keys
);
1037 $this->$k = $this->LastInsertID($db,$k);
1041 $this->_original
= $valarr;
1046 // returns 0 on error, 1 on update, -1 if no change in data (no update)
1049 $db = $this->DB(); if (!$db) return false;
1050 $table = $this->TableInfo();
1052 $where = $this->GenWhere($db, $table);
1055 $this->error("Where missing for table $table", "Update");
1063 foreach($table->flds
as $name=>$fld) {
1065 $val = $this->$name;
1068 if (isset($table->keys
[$name])) {
1072 if (is_null($val)) {
1073 if (isset($fld->not_null
) && $fld->not_null
) {
1074 if (isset($fld->default_value
) && strlen($fld->default_value
)) continue;
1076 $this->Error("Cannot set field $name to NULL","Update");
1082 if (isset($this->_original
[$i]) && $val == $this->_original
[$i]) {
1086 $pairs[] = $name.'='.$db->Param($cnt);
1091 if (!$cnt) return -1;
1092 $sql = 'UPDATE '.$this->_table
." SET ".implode(",",$pairs)." WHERE ".$where;
1093 $ok = $db->Execute($sql,$valarr);
1095 $this->_original
= $neworig;
1101 function GetAttributeNames()
1103 $table = $this->TableInfo();
1104 if (!$table) return false;
1105 return array_keys($table->flds
);
1110 function adodb_GetActiveRecordsClass(&$db, $class, $tableObj,$whereOrderBy,$bindarr, $primkeyArr,
1113 global $_ADODB_ACTIVE_DBS;
1115 if (empty($extra['loading'])) $extra['loading'] = ADODB_LAZY_AR
;
1117 $save = $db->SetFetchMode(ADODB_FETCH_NUM
);
1118 $table = &$tableObj->_table
;
1119 $tableInfo =& $tableObj->TableInfo();
1120 if(($k = reset($tableInfo->keys
)))
1124 $index = 0; $found = false;
1125 /** @todo Improve by storing once and for all in table metadata */
1126 /** @todo Also re-use info for hasManyId */
1127 foreach($tableInfo->flds
as $fld)
1129 if($fld->name
== $myId)
1137 $db->outp_throw("Unable to locate key $myId for $class in GetActiveRecordsClass()",'GetActiveRecordsClass');
1139 $qry = "select * from ".$table;
1140 if(ADODB_JOIN_AR
== $extra['loading'])
1142 if(!empty($relations['belongsTo']))
1144 foreach($relations['belongsTo'] as $foreignTable)
1146 if(($k = reset($foreignTable->TableInfo()->keys
)))
1152 $belongsToId = 'id';
1155 $qry .= ' LEFT JOIN '.$foreignTable->_table
.' ON '.
1156 $table.'.'.$foreignTable->foreignKey
.'='.
1157 $foreignTable->_table
.'.'.$belongsToId;
1160 if(!empty($relations['hasMany']))
1162 if(empty($relations['foreignName']))
1163 $db->outp_throw("Missing foreignName is relation specification in GetActiveRecordsClass()",'GetActiveRecordsClass');
1164 if(($k = reset($tableInfo->keys
)))
1169 foreach($relations['hasMany'] as $foreignTable)
1171 $qry .= ' LEFT JOIN '.$foreignTable->_table
.' ON '.
1172 $table.'.'.$hasManyId.'='.
1173 $foreignTable->_table
.'.'.$foreignTable->foreignKey
;
1177 if (!empty($whereOrderBy))
1178 $qry .= ' WHERE '.$whereOrderBy;
1179 if(isset($extra['limit']))
1182 if(isset($extra['offset'])) {
1183 $rs = $db->SelectLimit($qry, $extra['limit'], $extra['offset']);
1185 $rs = $db->SelectLimit($qry, $extra['limit']);
1189 $rows[] = $rs->fields
;
1194 $rows = $db->GetAll($qry,$bindarr);
1196 $db->SetFetchMode($save);
1200 if ($rows === false) {
1205 if (!isset($_ADODB_ACTIVE_DBS)) {
1206 include(ADODB_DIR
.'/adodb-active-record.inc.php');
1208 if (!class_exists($class)) {
1209 $db->outp_throw("Unknown class $class in GetActiveRecordsClass()",'GetActiveRecordsClass');
1212 $uniqArr = array(); // CFR Keep track of records for relations
1214 // arrRef will be the structure that knows about our objects.
1215 // It is an associative array.
1216 // We will, however, return arr, preserving regular 0.. order so that
1217 // obj[0] can be used by app developpers.
1219 $bTos = array(); // Will store belongTo's indices if any
1220 foreach($rows as $row) {
1222 $obj = new $class($table,$primkeyArr,$db);
1223 if ($obj->ErrorNo()){
1224 $db->_errorMsg
= $obj->ErrorMsg();
1228 // CFR: FIXME: Insane assumption here:
1229 // If the first column returned is an integer, then it's a 'id' field
1230 // And to make things a bit worse, I use intval() rather than is_int() because, in fact,
1231 // $row[0] is not an integer.
1233 // So, what does this whole block do?
1234 // When relationships are found, we perform JOINs. This is fast. But not accurate:
1235 // instead of returning n objects with their n' associated cousins,
1236 // we get n*n' objects. This code fixes this.
1237 // Note: to-many relationships mess around with the 'limit' parameter
1238 $rowId = intval($row[$index]);
1240 if(ADODB_WORK_AR
== $extra['loading'])
1242 $arrRef[$rowId] = $obj;
1243 $arr[] = &$arrRef[$rowId];
1244 if(!isset($indices))
1247 $indices .= ','.$rowId;
1248 if(!empty($relations['belongsTo']))
1250 foreach($relations['belongsTo'] as $foreignTable)
1252 $foreignTableRef = $foreignTable->foreignKey
;
1253 // First array: list of foreign ids we are looking for
1254 if(empty($bTos[$foreignTableRef]))
1255 $bTos[$foreignTableRef] = array();
1256 // Second array: list of ids found
1257 if(empty($obj->$foreignTableRef))
1259 if(empty($bTos[$foreignTableRef][$obj->$foreignTableRef]))
1260 $bTos[$foreignTableRef][$obj->$foreignTableRef] = array();
1261 $bTos[$foreignTableRef][$obj->$foreignTableRef][] = $obj;
1269 if(ADODB_JOIN_AR
== $extra['loading'])
1271 $isNewObj = !isset($uniqArr['_'.$row[0]]);
1273 $uniqArr['_'.$row[0]] = $obj;
1275 // TODO Copy/paste code below: bad!
1276 if(!empty($relations['hasMany']))
1278 foreach($relations['hasMany'] as $foreignTable)
1280 $foreignName = $foreignTable->foreignName
;
1281 if(!empty($obj->$foreignName))
1283 $masterObj = &$uniqArr['_'.$row[0]];
1284 // Assumption: this property exists in every object since they are instances of the same class
1285 if(!is_array($masterObj->$foreignName))
1288 $foreignObj = $masterObj->$foreignName;
1289 $masterObj->$foreignName = array(clone($foreignObj));
1294 $foreignObj = $obj->$foreignName;
1295 array_push($masterObj->$foreignName, clone($foreignObj));
1300 if(!empty($relations['belongsTo']))
1302 foreach($relations['belongsTo'] as $foreignTable)
1304 $foreignName = $foreignTable->foreignName
;
1305 if(!empty($obj->$foreignName))
1307 $masterObj = &$uniqArr['_'.$row[0]];
1308 // Assumption: this property exists in every object since they are instances of the same class
1309 if(!is_array($masterObj->$foreignName))
1312 $foreignObj = $masterObj->$foreignName;
1313 $masterObj->$foreignName = array(clone($foreignObj));
1318 $foreignObj = $obj->$foreignName;
1319 array_push($masterObj->$foreignName, clone($foreignObj));
1325 unset($obj); // We do not need this object itself anymore and do not want it re-added to the main array
1327 else if(ADODB_LAZY_AR
== $extra['loading'])
1329 // Lazy loading: we need to give AdoDb a hint that we have not really loaded
1330 // anything, all the while keeping enough information on what we wish to load.
1331 // Let's do this by keeping the relevant info in our relationship arrays
1332 // but get rid of the actual properties.
1333 // We will then use PHP's __get to load these properties on-demand.
1334 if(!empty($relations['hasMany']))
1336 foreach($relations['hasMany'] as $foreignTable)
1338 $foreignName = $foreignTable->foreignName
;
1339 if(!empty($obj->$foreignName))
1341 unset($obj->$foreignName);
1345 if(!empty($relations['belongsTo']))
1347 foreach($relations['belongsTo'] as $foreignTable)
1349 $foreignName = $foreignTable->foreignName
;
1350 if(!empty($obj->$foreignName))
1352 unset($obj->$foreignName);
1363 if(ADODB_WORK_AR
== $extra['loading'])
1365 // The best of both worlds?
1366 // Here, the number of queries is constant: 1 + n*relationship.
1367 // The second query will allow us to perform a good join
1368 // while preserving LIMIT etc.
1369 if(!empty($relations['hasMany']))
1371 foreach($relations['hasMany'] as $foreignTable)
1373 $foreignName = $foreignTable->foreignName
;
1374 $className = ucfirst($foreignTable->_singularize($foreignName));
1375 $obj = new $className();
1376 $dbClassRef = $foreignTable->foreignKey
;
1377 $objs = $obj->packageFind($dbClassRef.' IN ('.$indices.')');
1378 foreach($objs as $obj)
1380 if(!is_array($arrRef[$obj->$dbClassRef]->$foreignName))
1381 $arrRef[$obj->$dbClassRef]->$foreignName = array();
1382 array_push($arrRef[$obj->$dbClassRef]->$foreignName, $obj);
1387 if(!empty($relations['belongsTo']))
1389 foreach($relations['belongsTo'] as $foreignTable)
1391 $foreignTableRef = $foreignTable->foreignKey
;
1392 if(empty($bTos[$foreignTableRef]))
1394 if(($k = reset($foreignTable->TableInfo()->keys
)))
1400 $belongsToId = 'id';
1402 $origObjsArr = $bTos[$foreignTableRef];
1403 $bTosString = implode(',', array_keys($bTos[$foreignTableRef]));
1404 $foreignName = $foreignTable->foreignName
;
1405 $className = ucfirst($foreignTable->_singularize($foreignName));
1406 $obj = new $className();
1407 $objs = $obj->packageFind($belongsToId.' IN ('.$bTosString.')');
1408 foreach($objs as $obj)
1410 foreach($origObjsArr[$obj->$belongsToId] as $idx=>$origObj)
1412 $origObj->$foreignName = $obj;