Changeset 199 for trunk/trax/vendor/trax/active_record.php
- Timestamp:
- 05/04/06 19:52:43 (6 years ago)
- Files:
-
- 1 modified
-
trunk/trax/vendor/trax/active_record.php (modified) (46 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/trax/vendor/trax/active_record.php
r198 r199 63 63 64 64 /** 65 * Reference to the object returned by PEAR DB::Connect() 66 * 65 * Reference to the database object 66 * 67 * Reference to the database object returned by 68 * {@link http://pear.php.net/manual/en/package.database.db.db.connect.php PEAR DB::Connect()} 67 69 * @var object DB 68 * <b>FIXME: static should be after private</b> 69 */ 70 static private $db = null; 70 * see 71 * {@link http://pear.php.net/manual/en/package.database.db.php PEAR DB} 72 */ 73 private static $db = null; 71 74 72 75 /** … … 135 138 # Table associations 136 139 /** 137 * @todo Document this API140 * @todo Document this variable 138 141 * @var string[] 139 142 */ … … 141 144 142 145 /** 143 * @todo Document this API146 * @todo Document this variable 144 147 * @var string[] 145 148 */ … … 147 150 148 151 /** 149 * @todo Document this API152 * @todo Document this variable 150 153 * @var string[] 151 154 */ … … 153 156 154 157 /** 155 * @todo Document this API158 * @todo Document this variable 156 159 * @var string[] 157 160 */ … … 159 162 160 163 /** 161 * @todo Document this API164 * @todo Document this variable 162 165 * @var string[] 163 166 */ … … 271 274 272 275 /** 273 * @todo Document this API276 * @todo Document this variable 274 277 */ 275 278 public $display = 10; # Pagination how many numbers in the list << < 1 2 3 4 > >> … … 302 305 303 306 /** 304 * @todo Document this API307 * @todo Document this variable 305 308 */ 306 309 public $auto_save_habtm = true; # auto insert / update $has_and_belongs_to_many tables 307 310 308 311 /** 309 * @todo Document this API312 * @todo Document this variable 310 313 */ 311 314 public $auto_delete_habtm = true; # auto delete $has_and_belongs_to_many associations … … 359 362 360 363 /** 361 * @todo Document this API362 364 * Override get() if they do $model->some_association->field_name 363 365 * dynamically load the requested contents from the database. 366 * @todo Document this API 364 367 * @uses $belongs_to 365 368 * @uses get_association_type() … … 437 440 438 441 /** 439 * @todo Document this API440 442 * Override call() to dynamically call the database associations 443 * @todo Document this API 441 444 * @uses $aggregrations 442 445 * @uses aggregrate_all() … … 494 497 495 498 /** 496 * @todo Document this API497 499 * Returns a the name of the join table that would be used for the two 498 500 * tables. The join table name is decided from the alphabetical order … … 501 503 * Parameters: $first_table, $second_table: the names of two database tables, 502 504 * e.g. "movies" and "genres" 505 * @todo Document this API 503 506 */ 504 507 private function get_join_table_name($first_table, $second_table) { … … 511 514 512 515 /** 513 * @todo Document this API514 516 * Find all records using a "has_and_belongs_to_many" relationship 515 517 * (many-to-many with a join table in between). Note that you can also … … 523 525 * many rows you are interested in. E.g. movies 524 526 * Returns: An array of ActiveRecord objects. (e.g. Movie objects) 527 * @todo Document this API 525 528 */ 526 529 private function find_all_habtm($other_table_name, $parameters = null) { … … 572 575 573 576 /** 574 * @todo Document this API575 577 * Find all records using a "has_many" relationship (one-to-many) 576 578 * … … 578 580 * many rows relating to this object's id. 579 581 * Returns: An array of ActiveRecord objects. (e.g. Contact objects) 582 * @todo Document this API 580 583 */ 581 584 private function find_all_has_many($other_table_name, $parameters = null) { … … 629 632 630 633 /** 631 * @todo Document this API632 634 * Find all records using a "has_one" relationship (one-to-one) 633 635 * (the foreign key being in the other table) … … 635 637 * many rows relating to this object's id. 636 638 * Returns: An array of ActiveRecord objects. (e.g. Contact objects) 639 * @todo Document this API 637 640 */ 638 641 private function find_one_has_one($other_object_name, $parameters = null) { … … 660 663 661 664 /** 662 * @todo Document this API663 665 * Find all records using a "belongs_to" relationship (one-to-one) 664 666 * (the foreign key being in the table itself) … … 667 669 * Customer class, then $other_object_name 668 670 * will be "customer". 671 * @todo Document this API 669 672 */ 670 673 private function find_one_belongs_to($other_object_name, $parameters = null) { … … 749 752 750 753 /** 751 * @todo Document this API752 754 * get the attributes for a specific column. 753 755 * @uses $content_columns 756 * @todo Document this API 754 757 */ 755 758 function column_for_attribute($attribute) { … … 810 813 811 814 /** 812 * @todo Document this API813 815 * Only used if you want to do transactions and your db supports transactions 814 816 * 815 817 * @uses $db 818 * @todo Document this API 816 819 */ 817 820 function begin() { … … 821 824 822 825 /** 823 * @todo Document this API824 826 * Only used if you want to do transactions and your db supports transactions 825 827 * 826 828 * @uses $db 829 * @todo Document this API 827 830 */ 828 831 function commit() { … … 832 835 833 836 /** 834 * @todo Document this API835 837 * Only used if you want to do transactions and your db supports transactions 836 838 * 837 839 * @uses $db 840 * @todo Document this API 838 841 */ 839 842 function rollback() { … … 845 848 * 846 849 * @param string $sql SQL for the query command 847 * @return mixed {@link http://pear.php.net/manual/en/package.database.db.db-result.php object DB_result}850 * @return DB_result {@link http://pear.php.net/manual/en/package.database.db.db-result.php} 848 851 * Result set from query 849 852 * @uses $db 850 853 * @uses is_error() 854 * @uses log_query() 851 855 * @throws {@link ActiveRecordError} 852 856 */ … … 927 931 * 928 932 * If no rows match, an empty array is returned. 929 * @param string $conditionsSQL to use in the query. If933 * @param string SQL to use in the query. If 930 934 * $conditions contains "SELECT", then $orderings, $limit and 931 935 * $joins are ignored and the query is completely specified by … … 935 939 * include "WHERE $conditions". If $conditions is null, the 936 940 * entire table is returned. 937 * @param string $orderingsArgument to "ORDER BY" in query.941 * @param string Argument to "ORDER BY" in query. 938 942 * If specified, the query will include 939 943 * "ORDER BY $orderings". If omitted, no ordering will be 940 944 * applied. 941 * @param integer[] $limit Page, rows per page??? 945 * @param integer[] Page, rows per page??? 946 * @param string ??? 942 947 * @todo Document the $limit and $joins parameters 943 * @param string $joins ???944 948 * @uses $rows_per_page_default 945 949 * @uses $rows_per_page … … 955 959 * @throws {@link ActiveRecordError} 956 960 */ 957 function find_all($conditions = null, $orderings = null, $limit = null, $joins = null) { 961 function find_all($conditions = null, $orderings = null, 962 $limit = null, $joins = null) { 963 //error_log("find_all(".(is_null($conditions)?'null':$conditions) 964 // .', ' . (is_null($orderings)?'null':$orderings) 965 // .', ' . (is_null($limit)?'null':var_export($limit,true)) 966 // .', ' . (is_null($joins)?'null':$joins).')'); 967 968 // Is output to be generated in pages? 958 969 if (is_array($limit)) { 959 list(self::$page, self::$rows_per_page) = $limit; 960 if(self::$$page <= 0) self::$page = 1; 970 971 // Yes, get next page number and rows per page from argument 972 list($this->page, $this->rows_per_page) = $limit; 973 if($this->page <= 0) { 974 $this->page = 1; 975 } 961 976 # Default for rows_per_page: 962 if (self::$rows_per_page == null) self::$rows_per_page = self::$rows_per_page_default; 977 if ($this->rows_per_page == null) { 978 $this->rows_per_page = $this->rows_per_page_default; 979 } 963 980 # Set the LIMIT string segment for the SQL in the find_all 964 self::$offset = (self::$page - 1) * self::$rows_per_page;981 $this->offset = ($this->page - 1) * $this->rows_per_page; 965 982 # mysql 3.23 doesn't support OFFSET 966 983 //$limit = "$rows_per_page OFFSET $offset"; 967 $limit = self::$offset.", ".self::$rows_per_page; 984 $limit = $this->offset.", ".$this->rows_per_page; 985 986 // Remember that we're generating output in pages 987 // so $limit needs to be added to the query 968 988 $set_pages = true; 969 989 } 970 990 991 // Test source of SQL for query 971 992 if(stristr($conditions, "SELECT")) { 993 994 // SQL completely specified in argument so use it as is 972 995 $sql = $conditions; 973 996 } else { 997 998 // SQL will be built from specifications in argument 974 999 $sql = "SELECT * FROM ".$this->table_name." "; 1000 1001 // If join specified, include it 975 1002 if(!is_null($joins)) { 976 1003 if(substr($joins,0,4) != "LEFT") $sql .= ","; 977 1004 $sql .= " $joins "; 978 1005 } 979 if(!is_null($conditions)) $sql .= "WHERE $conditions "; 980 if(!is_null($orderings)) $sql .= "ORDER BY $orderings "; 1006 1007 // If conditions specified, include them 1008 if(!is_null($conditions)) { 1009 $sql .= "WHERE $conditions "; 1010 } 1011 1012 // If ordering specified, include it 1013 if(!is_null($orderings)) { 1014 $sql .= "ORDER BY $orderings "; 1015 } 1016 1017 // If limit specified, divide into pages 981 1018 if(!is_null($limit)) { 1019 // FIXME: Isn't the second test redundant? 982 1020 if($set_pages) { 983 //echo "ActiveRecord::find_all() - sql: $sql\n<br>"; 984 if(self::$is_error($rs = self::query($sql))) { 985 self::raise($rs->getMessage()); 1021 1022 // Send query to database 1023 //echo "query: $sql\n"; 1024 if($this->is_error($rs = $this->query($sql))) { 1025 1026 // Error returned, throw error exception 1027 $this->raise($rs->getMessage()); 1028 // Execution doesn't return here 986 1029 } else { 987 # Set number of total pages in result set without the LIMIT 1030 1031 # Set number of total pages in result set 1032 # without the LIMIT 988 1033 if($count = $rs->numRows()) 989 self::$pages = (($count % self::$rows_per_page) == 0) ? $count / self::$rows_per_page : floor($count / self::$rows_per_page) + 1; 1034 $this->pages = ( 1035 ($count % $this->rows_per_page) == 0) 1036 ? $count / $this->rows_per_page 1037 : floor($count / $this->rows_per_page) + 1; 990 1038 } 991 1039 } … … 995 1043 996 1044 //echo "ActiveRecord::find_all() - sql: $sql\n<br>"; 997 if(self::is_error($rs = self::query($sql))) { 998 self::raise($rs->getMessage()); 1045 //echo "query: $sql\n"; 1046 if($this->is_error($rs = $this->query($sql))) { 1047 $this->raise($rs->getMessage()); 999 1048 } 1000 1049 … … 1110 1159 1111 1160 /** 1112 * @todo Document this API1113 1161 * Reloads the attributes of this object from the database. 1114 1162 * @uses get_primary_key_conditions() 1163 * @todo Document this API 1115 1164 */ 1116 1165 function reload($conditions = null) { … … 1156 1205 1157 1206 /** 1158 * @todo Document this API1159 1207 * Finds the record from the passed id, instantly saves it with the passed attributes 1160 1208 * (if the validation permits it). Returns true on success and false on error. 1209 * @todo Document this API 1161 1210 */ 1162 1211 function update($id, $attributes, $dont_validate = false) { … … 1172 1221 1173 1222 /** 1174 * @todo Document this API1175 1223 * Updates all records with the SET-part of an SQL update statement in updates and 1176 1224 * returns an integer with the number of rows updates. A subset of the records can … … 1181 1229 * @uses query() 1182 1230 * @throws {@link ActiveRecordError} 1231 * @todo Document this API 1183 1232 */ 1184 1233 function update_all($updates, $conditions = null) { … … 1193 1242 1194 1243 /** 1195 * @todo Document this API1196 1244 * Save without valdiating anything. 1245 * @todo Document this API 1197 1246 */ 1198 1247 function save_without_validation($attributes = null) { … … 1412 1461 1413 1462 /** 1414 * @todo Document this API1415 1463 * Saves any associations objects assigned to this instance 1416 1464 * @uses $auto_save_associations 1465 * @todo Document this API 1417 1466 */ 1418 1467 private function save_associations() { … … 1435 1484 1436 1485 /** 1437 * @todo Document this API1438 1486 * save the association to the database 1487 * @todo Document this API 1439 1488 */ 1440 1489 private function save_association($object, $type) { … … 1446 1495 $foreign_key = Inflector::singularize($this->table_name)."_id"; 1447 1496 $object->$foreign_key = $this->id; 1448 echo "fk:$foreign_key = $this->id<br>";1497 //echo "fk:$foreign_key = $this->id<br>"; 1449 1498 break; 1450 1499 } … … 1454 1503 1455 1504 /** 1456 * @todo Document this API1457 1505 * Deletes the record with the given $id or if you have done a 1458 1506 * $model = $model->find($id), then $model->delete() it will delete … … 1460 1508 * to delete(). If an array of ids is provided, all ids in array are deleted. 1461 1509 * @uses $errors 1510 * @todo Document this API 1462 1511 */ 1463 1512 function delete($id = null) { … … 1526 1575 1527 1576 /** 1528 * @todo Document this API1529 *1530 1577 * @uses $has_and_belongs_to_many 1578 * @todo Document this API 1531 1579 */ 1532 1580 private function set_habtm_attributes($attributes) { … … 1559 1607 /** 1560 1608 * 1561 * @todo Document this API1562 1609 * @uses is_error() 1563 1610 * @uses query() 1564 1611 * @throws {@link ActiveRecordError} 1612 * @todo Document this API 1565 1613 */ 1566 1614 private function add_habtm_records($this_foreign_value) { … … 1593 1641 1594 1642 /** 1595 * @todo Document this API1596 1643 * 1597 1644 * @uses is_error() 1598 1645 * @uses query() 1599 1646 * @throws {@link ActiveRecordError} 1647 * @todo Document this API 1600 1648 */ 1601 1649 private function delete_habtm_records($this_foreign_value) { … … 1812 1860 $return[$key] = $value; 1813 1861 } 1814 //$return[$key] = self::$db->quoteSmart($value);1862 //$return[$key] = $this->$db->quoteSmart($value); 1815 1863 } 1816 1864 return $return; … … 2167 2215 2168 2216 /** 2169 * @todo Document this API2170 2217 * Overwrite this method for validation checks on all saves and 2171 2218 * use $this->errors[] = "My error message."; or 2172 2219 * for invalid attributes $this->errors['attribute'] = "Attribute is invalid."; 2220 * @todo Document this API 2173 2221 */ 2174 2222 function validate() {} 2175 2223 2176 2224 /** 2177 * @todo Document this API2178 2225 * Override this method for validation checks used only on creation. 2226 * @todo Document this API 2179 2227 */ 2180 2228 function validate_on_create() {} 2181 2229 2182 2230 /** 2183 * @todo Document this API2184 2231 * Override this method for validation checks used only on updates. 2232 * @todo Document this API 2185 2233 */ 2186 2234 function validate_on_update() {} 2187 2235 2188 2236 /** 2189 * @todo Document this API2190 2237 * Is called before validate(). 2238 * @todo Document this API 2191 2239 */ 2192 2240 function before_validation() {} 2193 2241 2194 2242 /** 2195 * @todo Document this API2196 2243 * Is called after validate(). 2244 * @todo Document this API 2197 2245 */ 2198 2246 function after_validation() {} 2199 2247 2200 2248 /** 2201 * @todo Document this API2202 2249 * Is called before validate() on new objects that haven't been saved yet (no record exists). 2250 * @todo Document this API 2203 2251 */ 2204 2252 function before_validation_on_create() {} 2205 2253 2206 2254 /** 2207 * @todo Document this API2208 2255 * Is called after validate() on new objects that haven't been saved yet (no record exists). 2256 * @todo Document this API 2209 2257 */ 2210 2258 function after_validation_on_create() {} 2211 2259 2212 2260 /** 2213 * @todo Document this API2214 2261 * Is called before validate() on existing objects that has a record. 2262 * @todo Document this API 2215 2263 */ 2216 2264 function before_validation_on_update() {} 2217 2265 2218 2266 /** 2219 * @todo Document this API2220 2267 * Is called after validate() on existing objects that has a record. 2268 * @todo Document this API 2221 2269 */ 2222 2270 function after_validation_on_update() {} 2223 2271 2224 2272 /** 2225 * @todo Document this API2226 2273 * Is called before save() (regardless of whether its a create or update save) 2274 * @todo Document this API 2227 2275 */ 2228 2276 function before_save() {} 2229 2277 2230 2278 /** 2231 * @todo Document this API2232 2279 * Is called after save (regardless of whether its a create or update save). 2280 * @todo Document this API 2233 2281 */ 2234 2282 function after_save() {} 2235 2283 2236 2284 /** 2237 * @todo Document this API2238 2285 * Is called before save() on new objects that havent been saved yet (no record exists). 2286 * @todo Document this API 2239 2287 */ 2240 2288 function before_create() {} 2241 2289 2242 2290 /** 2243 * @todo Document this API2244 2291 * Is called after save() on new objects that havent been saved yet (no record exists). 2292 * @todo Document this API 2245 2293 */ 2246 2294 function after_create() {} 2247 2295 2248 2296 /** 2249 * @todo Document this API2250 2297 * Is called before save() on existing objects that has a record. 2298 * @todo Document this API 2251 2299 */ 2252 2300 function before_update() {} 2253 2301 2254 2302 /** 2255 * @todo Document this API2256 2303 * Is called after save() on existing objects that has a record. 2304 * @todo Document this API 2257 2305 */ 2258 2306 function after_update() {} 2259 2307 2260 2308 /** 2261 * @todo Document this API2262 2309 * Is called before delete(). 2310 * @todo Document this API 2263 2311 */ 2264 2312 function before_delete() {} 2265 2313 2266 2314 /** 2267 * @todo Document this API2268 2315 * Is called after delete(). 2316 * @todo Document this API 2269 2317 */ 2270 2318 function after_delete() {} 2271 2319 2320 /** 2321 * Log SQL query in development mode 2322 * 2323 * If running in development mode, log the query to $GLOBAL 2324 * @param string SQL to be logged 2325 */ 2272 2326 function log_query($sql) { 2273 2327 if(TRAX_MODE == "development" && $sql) { … … 2277 2331 2278 2332 /** 2279 * @todo Document this API2280 2333 * Paging html functions 2334 * @todo Document this API 2281 2335 */ 2282 2336 function limit_select($controller =null, $additional_query = null) { … … 2299 2353 * @todo Document this API 2300 2354 * 2355 * @return string HTML to link to previous and next pages 2356 * @uses $display 2357 * @uses $page 2358 * @uses $pages 2359 * @uses $paging_extra_params 2360 * @uses rows_per_page 2301 2361 */ 2302 2362 function page_list(){
