PHP on T R A X
Rapid Application Development Made Easy
Show
Ignore:
Timestamp:
04/25/09 09:50:22 (11 months ago)
Author:
john
Message:

updates and bug fixes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/trax/vendor/trax/active_record.php

    r308 r311  
    693693                $result = $this->aggregate_all($method_name, $parameters); 
    694694            } 
     695            # check for the named scopes being called as a function 
     696            elseif(array_key_exists($method_name, $this->named_scope) && is_array($this->named_scope[$method_name])) { 
     697                $result = $this->find_all(array_merge($this->named_scope[$method_name], (array)$parameters)); 
     698            }             
    695699            # check for the find_all_by_* magic functions 
    696700            elseif(strlen($method_name) > 11 && substr($method_name, 0, 11) == "find_all_by") { 
     
    869873            if(@array_key_exists("foreign_key", $parameters)) { 
    870874                $foreign_key = $parameters['foreign_key']; 
    871             }              
     875            }     
     876            if(@array_key_exists("primary_key", $parameters)) { 
     877                $this_primary_key = $parameters['primary_key']; 
     878            }                      
    872879            if(@array_key_exists("class_name", $parameters)) { 
    873880                $other_object_name = $parameters['class_name']; 
     
    892899        } else {           
    893900            # This class primary key 
    894             $this_primary_key = $this->primary_keys[0]; 
    895      
     901            if(!$this_primary_key) { 
     902                $this_primary_key = $this->primary_keys[0]; 
     903            } 
     904                 
    896905            if(!$foreign_key) { 
    897906                # this should end up being like user_id or account_id but if you specified 
     
    943952            if(@array_key_exists("foreign_key", $parameters)) { 
    944953                $foreign_key = $parameters['foreign_key']; 
    945             }          
     954            } 
     955            if(@array_key_exists("primary_key", $parameters)) { 
     956                $this_primary_key = $parameters['primary_key']; 
     957            }                      
    946958            if(@array_key_exists("class_name", $parameters)) { 
    947959                $other_object_name = $parameters['class_name']; 
     
    955967 
    956968        # This class primary key 
    957         $this_primary_key = $this->primary_keys[0]; 
     969        if(!$this_primary_key) { 
     970            $this_primary_key = $this->primary_keys[0]; 
     971        } 
    958972         
    959973        if(!$foreign_key) { 
     
    10631077    private function aggregate_all($aggregate_type, $parameters = null) { 
    10641078        $aggregate_type = strtoupper(substr($aggregate_type, 0, -4)); 
     1079        $distinct = strtolower($aggregate_type) == 'count' ? 'DISTINCT ' : ''; 
    10651080        #($parameters[0]) ? $field = $parameters[0] : $field = "*"; 
    1066         $field = (stristr($parameters[0], ".") ? $parameters[0] : "{$this->table_prefix}{$this->table_name}.".$this->primary_keys[0]); 
    1067         $sql = "SELECT {$aggregate_type}({$field}) AS agg_result FROM {$this->table_prefix}{$this->table_name} "; 
    1068          
     1081        $field = (stristr($parameters[0], ".") ? $parameters[0] : "{$this->table_prefix}{$this->table_name}.".$parameters[0]); 
     1082        $sql = "SELECT {$aggregate_type}({$distinct}{$field}) AS agg_result FROM {$this->table_prefix}{$this->table_name} ";         
    10691083        # Use any passed-in parameters 
    10701084        if(is_array($parameters[1])) { 
     
    10741088            $joins = $parameters[2]; 
    10751089        } 
    1076  
    10771090        if(!empty($joins)) $sql .= " $joins "; 
    10781091        if(!empty($conditions)) $sql .= " WHERE $conditions "; 
    1079  
    10801092        # echo "$aggregate_type sql:$sql<br>"; 
    10811093        //print_r($parameters[0]); 
    10821094        //echo $sql; 
    1083         if($this->is_error($rs = $this->query($sql, true))) { 
    1084             $this->raise($rs->getMessage()); 
    1085         } else { 
    1086             $row = $rs->fetchRow(); 
    1087             if($row["agg_result"]) { 
    1088                 return $row["agg_result"];     
    1089             } 
     1095        $rs = $this->query($sql, true); 
     1096        $row = $rs->fetchRow(); 
     1097        if($row["agg_result"]) { 
     1098            return $row["agg_result"];     
    10901099        } 
    10911100        return 0; 
     
    11821191            $sql = "SELECT {$column} FROM {$this->table_prefix}{$this->table_name} WHERE {$conditions} LIMIT 1"; 
    11831192            $this->log_query($sql); 
    1184             $db = $this->get_connection(true); 
     1193            $db =& $this->get_connection(true); 
    11851194            $result = $db->queryOne($sql); 
    11861195            if($this->is_error($result)) { 
     
    11971206     *  @todo Document this API 
    11981207     */ 
    1199     function begin($save_point = null) { 
     1208    function begin() { 
    12001209        # check if transaction are supported by this driver 
    12011210        if(self::$db->supports('transactions')) {         
     
    12811290        # Run the query 
    12821291        $this->log_query($sql); 
    1283         $db = $this->get_connection($read_only); 
     1292        $db =& $this->get_connection($read_only); 
    12841293        $rs =& $db->query($sql); 
    12851294        if ($this->is_error($rs)) { 
     
    13221331        $method_parts = explode("|", str_replace("_and_", "|AND|", $method_name)); 
    13231332        if(count($method_parts)) { 
     1333            $conditions = null; 
    13241334            $options = array(); 
    13251335            $create_fields = array(); 
     
    13391349            # If last param exists and is a string set it as the ORDER BY clause             
    13401350            # or if the last param is an array set it as the $options 
    1341             if($last_param = $parameters[++$param_index]) { 
     1351            ++$param_index; 
     1352            if(isset($parameters[$param_index]) && ($last_param = $parameters[$param_index])) { 
    13421353                if(is_string($last_param)) { 
    13431354                    $options['order'] = $last_param;         
     
    13471358            }   
    13481359            # Set the conditions 
    1349             if($options['conditions'] && $conditions) { 
     1360            if(isset($options['conditions']) && $conditions) { 
    13501361                $options['conditions'] = "(".$options['conditions'].") AND (".$conditions.")";     
    13511362            } else { 
     
    14381449                     && !is_null($this->default_scope['conditions'])) { 
    14391450                $sql .= "WHERE ".$this->default_scope['conditions']." "; 
     1451            } 
     1452             
     1453            # If GROUP BY was specified 
     1454            if(!is_null($group)) { 
     1455                $sql .= "GROUP BY {$group} "; 
     1456            } 
     1457             
     1458            # If HAVING clause is specified 
     1459            if(!is_null($having)) { 
     1460                $sql .= "HAVING {$having} "; 
    14401461            } 
    14411462 
     
    15601581        # echo "query: $sql\n"; 
    15611582        # error_log("ActiveRecord::find_all -> $sql"); 
    1562         if($this->is_error($rs = $this->query($sql, true))) { 
    1563             $this->raise($rs->getMessage()); 
    1564         } 
    1565  
     1583        $rs = $this->query($sql, true); 
     1584         
    15661585        $objects = array(); 
    15671586        $class_name = $this->get_class_name(); 
     
    17801799    function update_all($updates, $conditions = null) { 
    17811800        $sql = "UPDATE {$this->table_prefix}{$this->table_name} SET {$updates} WHERE {$conditions}"; 
    1782         $result = $this->query($sql); 
    1783         if ($this->is_error($result)) { 
    1784             $this->raise($result->getMessage()); 
    1785         } else { 
    1786             return true; 
    1787         } 
     1801        $this->query($sql); 
     1802        return true; 
    17881803    } 
    17891804 
     
    18961911        //echo "add_record: SQL: $sql<br>"; 
    18971912        //error_log("add_record: SQL: $sql"); 
    1898         $result = $this->query($sql); 
    1899          
    1900         if($this->is_error($result)) { 
    1901             $this->raise($result->getMessage()); 
    1902         } else { 
    1903             $habtm_result = true; 
    1904             $primary_key = $this->primary_keys[0]; 
    1905             # $id is now equivalent to the value in the id field that was inserted 
    1906             $primary_key_value = self::$db->getAfterID($primary_key_value, "{$this->table_prefix}{$this->table_name}", $this->primary_keys[0]); 
    1907             if($this->is_error($primary_key_value)) { 
    1908                 $this->raise($primary_key_value->getMessage()); 
    1909             }             
    1910             $this->$primary_key = $primary_key_value; 
    1911             if($primary_key_value != '') { 
    1912                 if($this->auto_save_habtm) { 
    1913                     $habtm_result = $this->add_habtm_records($primary_key_value); 
    1914                 } 
    1915                 $this->save_associations(); 
    1916             }           
    1917             return ($result && $habtm_result); 
    1918         } 
     1913        $result = $this->query($sql);   
     1914        $habtm_result = true; 
     1915        $primary_key = $this->primary_keys[0]; 
     1916        # $id is now equivalent to the value in the id field that was inserted 
     1917        $primary_key_value = self::$db->getAfterID($primary_key_value, "{$this->table_prefix}{$this->table_name}", $this->primary_keys[0]); 
     1918        if($this->is_error($primary_key_value)) { 
     1919            $this->raise($primary_key_value->getMessage()); 
     1920        }             
     1921        $this->$primary_key = $primary_key_value; 
     1922        if($primary_key_value != '') { 
     1923            if($this->auto_save_habtm) { 
     1924                $habtm_result = $this->add_habtm_records($primary_key_value); 
     1925            } 
     1926            $this->save_associations(); 
     1927        }           
     1928        return ($result && $habtm_result); 
    19191929    } 
    19201930 
     
    19491959        //error_log("update_record: SQL: $sql"); 
    19501960        $result = $this->query($sql); 
    1951         if($this->is_error($result)) { 
    1952             $this->raise($result->getMessage()); 
    1953         } else { 
    1954             $habtm_result = true; 
    1955             $primary_key = $this->primary_keys[0]; 
    1956             $primary_key_value = $this->$primary_key; 
    1957             if($primary_key_value > 0) {  
    1958                 if($this->auto_save_habtm) { 
    1959                     $habtm_result = $this->update_habtm_records($primary_key_value); 
    1960                 } 
    1961                 $this->save_associations(); 
    1962             }          
    1963             return ($result && $habtm_result); 
    1964         } 
     1961        $habtm_result = true; 
     1962        $primary_key = $this->primary_keys[0]; 
     1963        $primary_key_value = $this->$primary_key; 
     1964        if($primary_key_value > 0) {  
     1965            if($this->auto_save_habtm) { 
     1966                $habtm_result = $this->update_habtm_records($primary_key_value); 
     1967            } 
     1968            $this->save_associations(); 
     1969        }          
     1970        return ($result && $habtm_result); 
    19651971    } 
    19661972 
     
    21822188            return false; 
    21832189        } 
    2184  
    21852190        # Delete the record(s) 
    2186         if($this->is_error($rs = $this->query("DELETE FROM {$this->table_prefix}{$this->table_name} WHERE {$conditions}"))) { 
    2187             $this->raise($rs->getMessage()); 
    2188         } 
    2189          
     2191        $this->query("DELETE FROM {$this->table_prefix}{$this->table_name} WHERE {$conditions}"); 
     2192        # reset this to a new record     
    21902193        $this->new_record = true; 
    21912194        return true; 
     
    22552258                        $sql = "INSERT INTO $table_name ($fields) VALUES ($values)"; 
    22562259                        error_log("add_habtm_records: SQL: $sql"); 
    2257                         $result = $this->query($sql); 
    2258                         if ($this->is_error($result)) { 
    2259                             $this->raise($result->getMessage()); 
    2260                         } 
     2260                        $this->query($sql); 
    22612261                    } 
    22622262                } 
     
    22892289            $sql = "DELETE FROM {$habtm_table_name} WHERE {$this_foreign_key} = {$this_foreign_value}"; 
    22902290            //echo "delete_all_habtm_records: SQL: $sql<br>"; 
    2291             $result = $this->query($sql); 
    2292             if($this->is_error($result)) { 
    2293                 $this->raise($result->getMessage()); 
    2294             }             
     2291            $this->query($sql);            
    22952292        } 
    22962293    } 
     
    27942791        return $connection_name;  
    27952792    } 
     2793 
    27962794    /** 
    27972795     *  Gets the database connection whether its read only or read/write     
     
    28102808        } 
    28112809        return $db; 
     2810    } 
     2811 
     2812    /** 
     2813     *  Clears all database connections 
     2814     */     
     2815    function clear_all_connections() { 
     2816        self::$connection_pool = array(); 
     2817        self::$connection_pool_read_only = array(); 
    28122818    } 
    28132819 
     
    35173523    } 
    35183524 
     3525    /** 
     3526     *  For debugging to see what the attributes for this object are. 
     3527     * 
     3528     *  echo User => Array([id] => 1, [name] => John) 
     3529     */     
     3530    function __toString() { 
     3531        return print_r($this->get_attributes(), true); 
     3532    } 
     3533 
    35193534} 
    35203535