Ignore:
Timestamp:
09/26/13 15:41:49 (11 years ago)
Author:
angelo
Message:

Ticket #3491 - Compatibilizar Expresso com novas versoes do PHP

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sandbox/2.5.1-evolucao/phpgwapi/inc/adodb/adodb-active-record.inc.php

    r34 r8222  
    22/* 
    33 
    4 @version V4.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. 
    55  Latest version is available at http://adodb.sourceforge.net 
    66  
     
    1111  Active Record implementation. Superset of Zend Framework's. 
    1212   
    13   Version 0.07 
     13  Version 0.92 
    1414   
    1515  See http://www-128.ibm.com/developerworks/java/library/j-cb03076/?ca=dgr-lnxw01ActiveRecord  
     
    1717*/ 
    1818 
     19 
    1920global $_ADODB_ACTIVE_DBS; 
    2021global $ADODB_ACTIVE_CACHESECS; // set to true to enable caching of metadata such as field info 
    2122global $ACTIVE_RECORD_SAFETY; // set to false to disable safety checks 
     23global $ADODB_ACTIVE_DEFVALS; // use default values of table definition when creating new active record. 
    2224 
    2325// array of ADODB_Active_DB's, indexed by ADODB_Active_Record->_dbat 
    2426$_ADODB_ACTIVE_DBS = array(); 
    2527$ACTIVE_RECORD_SAFETY = true; 
     28$ADODB_ACTIVE_DEFVALS = false; 
     29$ADODB_ACTIVE_CACHESECS = 0; 
    2630 
    2731class ADODB_Active_DB { 
     
    3539        var $keys; // assoc array of primary keys, indexed by fieldname 
    3640        var $_created; // only used when stored as a cached file 
     41        var $_belongsTo = array(); 
     42        var $_hasMany = array(); 
    3743} 
    3844 
     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  
    3948// returns index into $_ADODB_ACTIVE_DBS 
    40 function ADODB_SetDatabaseAdapter(&$db) 
     49function ADODB_SetDatabaseAdapter(&$db, $index=false) 
    4150{ 
    4251        global $_ADODB_ACTIVE_DBS; 
     
    5261                 
    5362                $obj = new ADODB_Active_DB(); 
    54                 $obj->db =& $db; 
     63                $obj->db = $db; 
    5564                $obj->tables = array(); 
    5665                 
    57                 $_ADODB_ACTIVE_DBS[] = $obj; 
     66                if ($index == false) $index = sizeof($_ADODB_ACTIVE_DBS); 
     67 
     68                 
     69                $_ADODB_ACTIVE_DBS[$index] = $obj; 
    5870                 
    5971                return sizeof($_ADODB_ACTIVE_DBS)-1; 
     
    6274 
    6375class ADODB_Active_Record { 
     76        static $_changeNames = true; // dynamically pluralize table names 
     77        static $_quoteNames = false; 
     78         
     79        static $_foreignSuffix = '_id'; //  
    6480        var $_dbat; // associative index pointing to ADODB_Active_DB eg. $ADODB_Active_DBS[_dbat] 
    6581        var $_table; // tablename, if set in class definition then use it as table name 
     
    6985        var $_lasterr = false; // last error message 
    7086        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 
    7299        // 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; 
    82110        } 
    83111         
     
    96124                        else $table = $this->_pluralize(get_class($this)); 
    97125                } 
     126                $this->foreignName = strtolower(get_class($this)); // CFR: default foreign name 
    98127                if ($db) { 
    99128                        $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 
    106135                $this->_table = $table; 
    107136                $this->_tableat = $table; # reserved for setting the assoc value to a non-table name, eg. the sql string in future 
     137 
    108138                $this->UpdateActiveTable($pkeyarr); 
    109139        } 
     
    117147        function _pluralize($table) 
    118148        { 
     149                if (!ADODB_Active_Record::$_changeNames) return $table; 
     150 
    119151                $ut = strtoupper($table); 
    120152                $len = strlen($table); 
     
    136168        } 
    137169         
     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        } 
    138335        ////////////////////////////////// 
    139336         
     
    142339        { 
    143340        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]; 
    146344 
    147345                $table = $this->_table; 
     
    149347                $tableat = $this->_tableat; 
    150348                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 
    153355                                $this->$name = null; 
     356                        } 
    154357                        return; 
    155358                } 
    156                  
    157                 $db =& $activedb->db; 
     359                $db = $activedb->db; 
    158360                $fname = $ADODB_CACHE_DIR . '/adodb_' . $db->databaseType . '_active_'. $table . '.cache'; 
    159361                if (!$forceUpdate && $ADODB_ACTIVE_CACHESECS && $ADODB_CACHE_DIR && file_exists($fname)) { 
     
    165367                                // abs(rand()) randomizes deletion, reducing contention to delete/refresh file 
    166368                                // 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         
    167377                                $activedb->tables[$table] = $acttab; 
    168378                                 
     
    176386                $activetab->name = $table; 
    177387                 
     388                $save = $ADODB_FETCH_MODE; 
     389                $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; 
     390                if ($db->fetchMode !== false) $savem = $db->SetFetchMode(false); 
    178391                 
    179392                $cols = $db->MetaColumns($table); 
     393                 
     394                if (isset($savem)) $db->SetFetchMode($savem); 
     395                $ADODB_FETCH_MODE = $save; 
     396                 
    180397                if (!$cols) { 
    181398                        $this->Error("Invalid table name: $table",'UpdateActiveTable');  
     
    204421                        foreach($cols as $name => $fldobj) { 
    205422                                $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; 
    207427                                $attr[$name] = $fldobj; 
    208428                        } 
     
    215435                        foreach($cols as $name => $fldobj) { 
    216436                                $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; 
    218442                                $attr[$name] = $fldobj; 
    219443                        } 
     
    226450                        foreach($cols as $name => $fldobj) { 
    227451                                $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; 
    229457                                $attr[$name] = $fldobj; 
    230458                        } 
     
    244472                        adodb_write_file($fname,$s); 
    245473                } 
     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                } 
    246480                $activedb->tables[$table] = $activetab; 
    247481        } 
     
    263497                else { 
    264498                        $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; 
    265                         $db =& $activedb->db; 
     499                        $db = $activedb->db; 
    266500                } 
    267501                 
     
    297531 
    298532        // retrieve ADOConnection from _ADODB_Active_DBs 
    299         function &DB() 
     533        function DB() 
    300534        { 
    301535        global $_ADODB_ACTIVE_DBS; 
     
    307541                } 
    308542                $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; 
    309                 $db =& $activedb->db; 
     543                $db = $activedb->db; 
    310544                return $db; 
    311545        } 
     
    315549        { 
    316550        global $_ADODB_ACTIVE_DBS; 
    317          
    318551                $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; 
    319                 $table =& $activedb->tables[$this->_tableat]; 
     552                $table = $activedb->tables[$this->_tableat]; 
    320553                return $table; 
    321554        } 
     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 
    322567         
    323568        // set a numeric array (using natural table field ordering) as object properties 
     
    326571        global $ACTIVE_RECORD_SAFETY; 
    327572         
    328                 $db =& $this->DB(); 
     573                $db = $this->DB(); 
    329574                 
    330575                if (!$row) { 
     
    335580                $this->_saved = true; 
    336581                 
    337                 $table =& $this->TableInfo(); 
     582                $table = $this->TableInfo(); 
    338583                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) { 
    339593                        $this->Error("Table structure of $this->_table has changed","Load"); 
    340594                        return false; 
    341595                } 
    342                  
    343                 $cnt = 0; 
     596            # </AP> 
     597                } 
     598        else 
     599                        $keys = array_keys($row); 
     600                         
     601        # <AP> 
     602        reset($keys); 
     603        $this->_original = array(); 
    344604                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> 
    349612                return true; 
    350613        } 
     
    369632        { 
    370633                switch($t) { 
    371                 case 'D': 
     634                case 'L': 
     635                        if (strpos($db->databaseType,'postgres') !== false) return $db->qstr($val); 
     636                case 'D':        
    372637                case 'T': 
    373638                        if (empty($val)) return 'null'; 
    374                          
     639                 
     640                case 'B':        
     641                case 'N': 
    375642                case 'C': 
    376643                case 'X': 
    377644                        if (is_null($val)) return 'null'; 
    378645                         
    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) != "'")) {  
    380648                                return $db->qstr($val); 
    381649                                break; 
     
    403671         
    404672         
     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         
    405680        //------------------------------------------------------------ Public functions below 
    406681         
    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; 
    410687                $this->_where = $where; 
    411688                 
    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; 
    415704                 
    416705                return $this->Set($row); 
    417706        } 
     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    } 
    418730         
    419731        // false on error 
     
    426738        } 
    427739         
     740         
    428741        // false on error 
    429742        function Insert() 
    430743        { 
    431                 $db =& $this->DB(); if (!$db) return false; 
     744                $db = $this->DB(); if (!$db) return false; 
    432745                $cnt = 0; 
    433                 $table =& $this->TableInfo(); 
     746                $table = $this->TableInfo(); 
    434747                 
    435748                $valarr = array(); 
     
    439752                foreach($table->flds as $name=>$fld) { 
    440753                        $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)) { 
    442755                                $valarr[] = $val; 
    443                                 $names[] = $name; 
     756                                $names[] = $this->_QName($name,$db); 
    444757                                $valstr[] = $db->Param($cnt); 
    445758                                $cnt += 1; 
     
    479792        function Delete() 
    480793        { 
    481                 $db =& $this->DB(); if (!$db) return false; 
    482                 $table =& $this->TableInfo(); 
     794                $db = $this->DB(); if (!$db) return false; 
     795                $table = $this->TableInfo(); 
    483796                 
    484797                $where = $this->GenWhere($db,$table); 
     
    490803         
    491804        // 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); 
    496809                return $arr; 
    497810        } 
     
    502815        global $ADODB_ASSOC_CASE; 
    503816                 
    504                 $db =& $this->DB(); if (!$db) return false; 
    505                 $table =& $this->TableInfo(); 
     817                $db = $this->DB(); if (!$db) return false; 
     818                $table = $this->TableInfo(); 
    506819                 
    507820                $pkey = $table->keys; 
     
    522835                continue; 
    523836            } 
     837                         
     838                        if (is_array($val)) continue; 
     839                         
    524840                        $t = $db->MetaType($fld->type); 
    525841                        $arr[$name] = $this->doquote($db,$val,$t); 
     
    533849                        foreach($pkey as $k => $v) 
    534850                                $pkey[$k] = strtolower($v); 
    535                 elseif ($ADODB_ASSOC_CASE == 0)  
     851                elseif ($ADODB_ASSOC_CASE == 1)  
    536852                        foreach($pkey as $k => $v) 
    537853                                $pkey[$k] = strtoupper($v); 
     
    554870                        } 
    555871                         
    556                         $this->_original =& $valarr; 
     872                        $this->_original = $valarr; 
    557873                }  
    558874                return $ok; 
     
    562878        function Update() 
    563879        { 
    564                 $db =& $this->DB(); if (!$db) return false; 
    565                 $table =& $this->TableInfo(); 
     880                $db = $this->DB(); if (!$db) return false; 
     881                $table = $this->TableInfo(); 
    566882                 
    567883                $where = $this->GenWhere($db, $table); 
     
    581897                        $neworig[] = $val; 
    582898                         
    583                         if (isset($table->keys[$name])) { 
     899                        if (isset($table->keys[$name]) || is_array($val))  
    584900                                continue; 
    585                         } 
    586901                         
    587902                        if (is_null($val)) { 
     
    594909                                } 
    595910                        } 
    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                         
    600916                        $valarr[] = $val; 
    601                         $pairs[] = $name.'='.$db->Param($cnt); 
     917                        $pairs[] = $this->_QName($name,$db).'='.$db->Param($cnt); 
    602918                        $cnt += 1; 
    603919                } 
     
    608924                $ok = $db->Execute($sql,$valarr); 
    609925                if ($ok) { 
    610                         $this->_original =& $neworig; 
     926                        $this->_original = $neworig; 
    611927                        return 1; 
    612928                } 
     
    616932        function GetAttributeNames() 
    617933        { 
    618                 $table =& $this->TableInfo(); 
     934                $table = $this->TableInfo(); 
    619935                if (!$table) return false; 
    620936                return array_keys($table->flds); 
     
    623939}; 
    624940 
     941function adodb_GetActiveRecordsClass(&$db, $class, $table,$whereOrderBy,$bindarr, $primkeyArr, 
     942                        $extra) 
     943{ 
     944global $_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} 
    6251002?> 
Note: See TracChangeset for help on using the changeset viewer.