Changeset 311 for trunk/trax/vendor/trax/active_record.php
- Timestamp:
- 04/25/09 09:50:22 (11 months ago)
- Files:
-
- trunk/trax/vendor/trax/active_record.php (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/trax/vendor/trax/active_record.php
r308 r311 693 693 $result = $this->aggregate_all($method_name, $parameters); 694 694 } 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 } 695 699 # check for the find_all_by_* magic functions 696 700 elseif(strlen($method_name) > 11 && substr($method_name, 0, 11) == "find_all_by") { … … 869 873 if(@array_key_exists("foreign_key", $parameters)) { 870 874 $foreign_key = $parameters['foreign_key']; 871 } 875 } 876 if(@array_key_exists("primary_key", $parameters)) { 877 $this_primary_key = $parameters['primary_key']; 878 } 872 879 if(@array_key_exists("class_name", $parameters)) { 873 880 $other_object_name = $parameters['class_name']; … … 892 899 } else { 893 900 # 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 896 905 if(!$foreign_key) { 897 906 # this should end up being like user_id or account_id but if you specified … … 943 952 if(@array_key_exists("foreign_key", $parameters)) { 944 953 $foreign_key = $parameters['foreign_key']; 945 } 954 } 955 if(@array_key_exists("primary_key", $parameters)) { 956 $this_primary_key = $parameters['primary_key']; 957 } 946 958 if(@array_key_exists("class_name", $parameters)) { 947 959 $other_object_name = $parameters['class_name']; … … 955 967 956 968 # 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 } 958 972 959 973 if(!$foreign_key) { … … 1063 1077 private function aggregate_all($aggregate_type, $parameters = null) { 1064 1078 $aggregate_type = strtoupper(substr($aggregate_type, 0, -4)); 1079 $distinct = strtolower($aggregate_type) == 'count' ? 'DISTINCT ' : ''; 1065 1080 #($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} "; 1069 1083 # Use any passed-in parameters 1070 1084 if(is_array($parameters[1])) { … … 1074 1088 $joins = $parameters[2]; 1075 1089 } 1076 1077 1090 if(!empty($joins)) $sql .= " $joins "; 1078 1091 if(!empty($conditions)) $sql .= " WHERE $conditions "; 1079 1080 1092 # echo "$aggregate_type sql:$sql<br>"; 1081 1093 //print_r($parameters[0]); 1082 1094 //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"]; 1090 1099 } 1091 1100 return 0; … … 1182 1191 $sql = "SELECT {$column} FROM {$this->table_prefix}{$this->table_name} WHERE {$conditions} LIMIT 1"; 1183 1192 $this->log_query($sql); 1184 $db = $this->get_connection(true);1193 $db =& $this->get_connection(true); 1185 1194 $result = $db->queryOne($sql); 1186 1195 if($this->is_error($result)) { … … 1197 1206 * @todo Document this API 1198 1207 */ 1199 function begin( $save_point = null) {1208 function begin() { 1200 1209 # check if transaction are supported by this driver 1201 1210 if(self::$db->supports('transactions')) { … … 1281 1290 # Run the query 1282 1291 $this->log_query($sql); 1283 $db = $this->get_connection($read_only);1292 $db =& $this->get_connection($read_only); 1284 1293 $rs =& $db->query($sql); 1285 1294 if ($this->is_error($rs)) { … … 1322 1331 $method_parts = explode("|", str_replace("_and_", "|AND|", $method_name)); 1323 1332 if(count($method_parts)) { 1333 $conditions = null; 1324 1334 $options = array(); 1325 1335 $create_fields = array(); … … 1339 1349 # If last param exists and is a string set it as the ORDER BY clause 1340 1350 # 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])) { 1342 1353 if(is_string($last_param)) { 1343 1354 $options['order'] = $last_param; … … 1347 1358 } 1348 1359 # Set the conditions 1349 if( $options['conditions']&& $conditions) {1360 if(isset($options['conditions']) && $conditions) { 1350 1361 $options['conditions'] = "(".$options['conditions'].") AND (".$conditions.")"; 1351 1362 } else { … … 1438 1449 && !is_null($this->default_scope['conditions'])) { 1439 1450 $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} "; 1440 1461 } 1441 1462 … … 1560 1581 # echo "query: $sql\n"; 1561 1582 # 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 1566 1585 $objects = array(); 1567 1586 $class_name = $this->get_class_name(); … … 1780 1799 function update_all($updates, $conditions = null) { 1781 1800 $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; 1788 1803 } 1789 1804 … … 1896 1911 //echo "add_record: SQL: $sql<br>"; 1897 1912 //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); 1919 1929 } 1920 1930 … … 1949 1959 //error_log("update_record: SQL: $sql"); 1950 1960 $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); 1965 1971 } 1966 1972 … … 2182 2188 return false; 2183 2189 } 2184 2185 2190 # 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 2190 2193 $this->new_record = true; 2191 2194 return true; … … 2255 2258 $sql = "INSERT INTO $table_name ($fields) VALUES ($values)"; 2256 2259 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); 2261 2261 } 2262 2262 } … … 2289 2289 $sql = "DELETE FROM {$habtm_table_name} WHERE {$this_foreign_key} = {$this_foreign_value}"; 2290 2290 //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); 2295 2292 } 2296 2293 } … … 2794 2791 return $connection_name; 2795 2792 } 2793 2796 2794 /** 2797 2795 * Gets the database connection whether its read only or read/write … … 2810 2808 } 2811 2809 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(); 2812 2818 } 2813 2819 … … 3517 3523 } 3518 3524 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 3519 3534 } 3520 3535

