core

Changeset 24913

Show
Ignore:
Timestamp:
11/26/08 10:17:00 (6 weeks ago)
Author:
Guite
Message:

fixed #387 - adodb updates (updated adodb to 5.06a for 2.0, cleaned up redundant docs, overhauled documentation of customizations)

Location:
development/main/includes/classes/adodb
Files:
7 added
9 removed
113 modified
1 moved

Legend:

Unmodified
Added
Removed
  • development/main/includes/classes/adodb/adodb-active-record.inc.php

    r22485 r24913  
    22/* 
    33 
    4 @version V5.00 05 Feb 2007   (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved. 
     4@version V5.06 16 Oct 2008   (c) 2000-2008 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.90 
    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; 
    2629 
    2730class ADODB_Active_DB { 
     
    3538        var $keys; // assoc array of primary keys, indexed by fieldname 
    3639        var $_created; // only used when stored as a cached file 
     40        var $_belongsTo = array(); 
     41        var $_hasMany = array(); 
    3742} 
    3843 
     
    6267 
    6368class ADODB_Active_Record { 
     69        static $_changeNames = true; // dynamically pluralize table names 
     70        static $_foreignSuffix = '_id'; //  
    6471        var $_dbat; // associative index pointing to ADODB_Active_DB eg. $ADODB_Active_DBS[_dbat] 
    6572        var $_table; // tablename, if set in class definition then use it as table name 
     
    6976        var $_lasterr = false; // last error message 
    7077        var $_original = false; // the original values loaded or inserted, refreshed on update 
    71          
     78 
     79        var $foreignName; // CFR: class name when in a relationship 
     80 
     81        static function UseDefaultValues($bool=null) 
     82        { 
     83        global $ADODB_ACTIVE_DEFVALS; 
     84                if (isset($bool)) $ADODB_ACTIVE_DEFVALS = $bool; 
     85                return $ADODB_ACTIVE_DEFVALS; 
     86        } 
     87 
    7288        // should be static 
    73         function SetDatabaseAdapter(&$db)  
     89        static function SetDatabaseAdapter(&$db)  
    7490        { 
    7591                return ADODB_SetDatabaseAdapter($db); 
    7692        } 
    7793         
    78         // php4 constructor 
    79         function ADODB_Active_Record($table = false, $pkeyarr=false, $db=false) 
    80         { 
    81                 ADODB_Active_Record::__construct($table,$pkeyarr,$db); 
     94         
     95        public function __set($name, $value) 
     96        { 
     97                $name = str_replace(' ', '_', $name); 
     98                $this->$name = $value; 
    8299        } 
    83100         
     
    96113                        else $table = $this->_pluralize(get_class($this)); 
    97114                } 
     115                $this->foreignName = strtolower(get_class($this)); // CFR: default foreign name 
    98116                if ($db) { 
    99117                        $this->_dbat = ADODB_Active_Record::SetDatabaseAdapter($db); 
     
    106124                $this->_table = $table; 
    107125                $this->_tableat = $table; # reserved for setting the assoc value to a non-table name, eg. the sql string in future 
     126 
    108127                $this->UpdateActiveTable($pkeyarr); 
    109128        } 
     
    117136        function _pluralize($table) 
    118137        { 
     138                if (!ADODB_Active_Record::$_changeNames) return $table; 
     139 
    119140                $ut = strtoupper($table); 
    120141                $len = strlen($table); 
     
    136157        } 
    137158         
     159        // CFR Lamest singular inflector ever - @todo Make it real! 
     160        // Note: There is an assumption here...and it is that the argument's length >= 4 
     161        function _singularize($tables) 
     162        { 
     163         
     164                if (!ADODB_Active_Record::$_changeNames) return $table; 
     165         
     166                $ut = strtoupper($tables); 
     167                $len = strlen($tables); 
     168                if($ut[$len-1] != 'S') 
     169                        return $tables; // I know...forget oxen 
     170                if($ut[$len-2] != 'E') 
     171                        return substr($tables, 0, $len-1); 
     172                switch($ut[$len-3]) 
     173                { 
     174                        case 'S': 
     175                        case 'X': 
     176                                return substr($tables, 0, $len-2); 
     177                        case 'I': 
     178                                return substr($tables, 0, $len-3) . 'y'; 
     179                        case 'H'; 
     180                                if($ut[$len-4] == 'C' || $ut[$len-4] == 'S') 
     181                                        return substr($tables, 0, $len-2); 
     182                        default: 
     183                                return substr($tables, 0, $len-1); // ? 
     184                } 
     185        } 
     186 
     187        function hasMany($foreignRef, $foreignKey = false) 
     188        { 
     189                $ar = new ADODB_Active_Record($foreignRef); 
     190                $ar->foreignName = $foreignRef; 
     191                $ar->UpdateActiveTable(); 
     192                $ar->foreignKey = ($foreignKey) ? $foreignKey : $foreignRef.ADODB_Active_Record::$_foreignSuffix; 
     193                $table =& $this->TableInfo(); 
     194                $table->_hasMany[$foreignRef] = $ar; 
     195        #       $this->$foreignRef = $this->_hasMany[$foreignRef]; // WATCHME Removed assignment by ref. to please __get() 
     196        } 
     197 
     198        function belongsTo($foreignRef,$foreignKey=false, $parentKey='') 
     199        { 
     200                global $inflector; 
     201 
     202                $ar = new ADODB_Active_Record($this->_pluralize($foreignRef)); 
     203                $ar->foreignName = $foreignRef; 
     204                $ar->parentKey = $parentKey; 
     205                $ar->UpdateActiveTable(); 
     206                $ar->foreignKey = ($foreignKey) ? $foreignKey : $foreignRef.ADODB_Active_Record::$_foreignSuffix; 
     207                 
     208                $table =& $this->TableInfo(); 
     209                $table->_belongsTo[$foreignRef] = $ar; 
     210        #       $this->$foreignRef = $this->_belongsTo[$foreignRef]; 
     211        } 
     212 
     213        /** 
     214         * __get Access properties - used for lazy loading 
     215         *  
     216         * @param mixed $name  
     217         * @access protected 
     218         * @return mixed 
     219         */ 
     220         function __get($name) 
     221        { 
     222                return $this->LoadRelations($name, '', -1, -1); 
     223        } 
     224         
     225        /** 
     226         * @param string $name  
     227         * @param string $whereOrderBy : eg. ' AND field1 = value ORDER BY field2' 
     228         * @param offset 
     229         * @param limit 
     230         * @return mixed 
     231         */ 
     232        function LoadRelations($name, $whereOrderBy='', $offset=-1,$limit=-1) 
     233        { 
     234                $extras = array(); 
     235                $table = $this->TableInfo(); 
     236                if ($limit >= 0) $extras['limit'] = $limit; 
     237                if ($offset >= 0) $extras['offset'] = $offset; 
     238                 
     239                if (strlen($whereOrderBy))  
     240                        if (!preg_match('/^[ \n\r]*AND/i',$whereOrderBy)) 
     241                                if (!preg_match('/^[ \n\r]*ORDER[ \n\r]/i',$whereOrderBy)) 
     242                                        $whereOrderBy = 'AND '.$whereOrderBy; 
     243                                 
     244                if(!empty($table->_belongsTo[$name])) 
     245                { 
     246                        $obj = $table->_belongsTo[$name]; 
     247                        $columnName = $obj->foreignKey; 
     248                        if(empty($this->$columnName)) 
     249                                $this->$name = null; 
     250                        else 
     251                        { 
     252                                if ($obj->parentKey) $key = $obj->parentKey; 
     253                                else $key = reset($table->keys); 
     254                                 
     255                                $arrayOfOne = $obj->Find($key.'='.$this->$columnName.' '.$whereOrderBy,false,false,$extras); 
     256                                if ($arrayOfOne) { 
     257                                        $this->$name = $arrayOfOne[0]; 
     258                                        return $arrayOfOne[0]; 
     259                                } 
     260                        } 
     261                } 
     262                if(!empty($table->_hasMany[$name])) 
     263                {        
     264                        $obj = $table->_hasMany[$name]; 
     265                        $objs = $obj->Find($obj->foreignKey.'='.$this->id. ' '.$whereOrderBy,false,false,$extras); 
     266                        if (!$objs) $objs = array(); 
     267                        $this->$name = $objs; 
     268                        return $objs; 
     269                } 
     270                 
     271                return array(); 
     272        } 
    138273        ////////////////////////////////// 
    139274         
     
    142277        { 
    143278        global $ADODB_ASSOC_CASE,$_ADODB_ACTIVE_DBS , $ADODB_CACHE_DIR, $ADODB_ACTIVE_CACHESECS; 
    144          
     279        global $ADODB_ACTIVE_DEFVALS; 
     280 
    145281                $activedb = $_ADODB_ACTIVE_DBS[$this->_dbat]; 
    146282 
     
    149285                $tableat = $this->_tableat; 
    150286                if (!$forceUpdate && !empty($tables[$tableat])) { 
     287 
    151288                        $tobj = $tables[$tableat]; 
    152                         foreach($tobj->flds as $name => $fld)  
     289                        foreach($tobj->flds as $name => $fld) { 
     290                        if ($ADODB_ACTIVE_DEFVALS && isset($fld->default_value))  
     291                                $this->$name = $fld->default_value; 
     292                        else 
    153293                                $this->$name = null; 
     294                        } 
    154295                        return; 
    155296                } 
    156                  
    157297                $db = $activedb->db; 
    158298                $fname = $ADODB_CACHE_DIR . '/adodb_' . $db->databaseType . '_active_'. $table . '.cache'; 
     
    204344                        foreach($cols as $name => $fldobj) { 
    205345                                $name = strtolower($name); 
    206                                 $this->$name = null; 
     346                if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) 
     347                    $this->$name = $fldobj->default_value; 
     348                else 
     349                                        $this->$name = null; 
    207350                                $attr[$name] = $fldobj; 
    208351                        } 
     
    215358                        foreach($cols as $name => $fldobj) { 
    216359                                $name = strtoupper($name); 
    217                                 $this->$name = null; 
     360                
     361                if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) 
     362                    $this->$name = $fldobj->default_value; 
     363                else 
     364                                        $this->$name = null; 
    218365                                $attr[$name] = $fldobj; 
    219366                        } 
     
    226373                        foreach($cols as $name => $fldobj) { 
    227374                                $name = ($fldobj->name); 
    228                                 $this->$name = null; 
     375                 
     376                if ($ADODB_ACTIVE_DEFVALS && isset($fldobj->default_value)) 
     377                    $this->$name = $fldobj->default_value; 
     378                else 
     379                                        $this->$name = null; 
    229380                                $attr[$name] = $fldobj; 
    230381                        } 
     
    244395                        adodb_write_file($fname,$s); 
    245396                } 
     397                if (isset($activedb->tables[$table])) { 
     398                        $oldtab = $activedb->tables[$table]; 
     399                 
     400                        if ($oldtab) $activetab->_belongsTo = $oldtab->_belongsTo; 
     401                        if ($oldtab) $activetab->_hasMany = $oldtab->_hasMany; 
     402                } 
    246403                $activedb->tables[$table] = $activetab; 
    247404        } 
     
    312469         
    313470        // retrieve ADODB_Active_Table 
    314         function TableInfo() 
     471        function &TableInfo() 
    315472        { 
    316473        global $_ADODB_ACTIVE_DBS; 
     
    321478        } 
    322479         
     480         
     481        // I have an ON INSERT trigger on a table that sets other columns in the table. 
     482        // So, I find that for myTable, I want to reload an active record after saving it. -- Malcolm Cook 
     483        function Reload() 
     484        { 
     485                $db =& $this->DB(); if (!$db) return false; 
     486                $table =& $this->TableInfo(); 
     487                $where = $this->GenWhere($db, $table); 
     488                return($this->Load($where)); 
     489        } 
     490 
     491         
    323492        // set a numeric array (using natural table field ordering) as object properties 
    324493        function Set(&$row) 
     
    337506                $table = $this->TableInfo(); 
    338507                if ($ACTIVE_RECORD_SAFETY && sizeof($table->flds) != sizeof($row)) { 
     508            # <AP> 
     509            $bad_size = TRUE; 
     510            if (sizeof($row) == 2 * sizeof($table->flds)) { 
     511                // Only keep string keys 
     512                $keys = array_filter(array_keys($row), 'is_string'); 
     513                if (sizeof($keys) == sizeof($table->flds)) 
     514                    $bad_size = FALSE; 
     515            } 
     516            if ($bad_size) { 
    339517                        $this->Error("Table structure of $this->_table has changed","Load"); 
    340518                        return false; 
    341519                } 
    342                  
    343                 $cnt = 0; 
     520            # </AP> 
     521                } 
     522        else 
     523                        $keys = array_keys($row); 
     524                         
     525        # <AP> 
     526        reset($keys); 
     527        $this->_original = array(); 
    344528                foreach($table->flds as $name=>$fld) { 
    345                         $this->$name = $row[$cnt]; 
    346                         $cnt += 1; 
    347                 } 
    348                 $this->_original