Changeset 223 for trunk/trax/vendor/trax
- Timestamp:
- 06/21/06 04:16:01 (6 years ago)
- Location:
- trunk/trax/vendor/trax
- Files:
-
- 2 modified
-
action_view/helpers/url_helper.php (modified) (1 diff)
-
active_record.php (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/trax/vendor/trax/action_view/helpers/url_helper.php
r207 r223 280 280 } 281 281 282 #if(count($options)) {283 #foreach($options as $key => $value) {284 #if(!strstr($key, ":")) {285 #$extra_params[$key] = $value;286 #}287 #}288 #}282 if(count($options)) { 283 foreach($options as $key => $value) { 284 if(!strstr($key, ":")) { 285 $extra_params[$key] = $value; 286 } 287 } 288 } 289 289 } 290 290 -
trunk/trax/vendor/trax/active_record.php
r215 r223 407 407 return null; 408 408 } 409 //error_log("association_type:$association_type"); 409 410 switch($association_type) { 410 411 case "has_many": … … 512 513 elseif(strlen($method_name) > 11 && substr($method_name, 0, 11) == "find_all_by") { 513 514 //echo "calling method: $method_name<br>"; 514 $result = $this->find_by($method_name, $parameters, true);515 $result = $this->find_by($method_name, $parameters, "all"); 515 516 } 516 517 # check for the find_by_* magic functions … … 518 519 //echo "calling method: $method_name<br>"; 519 520 $result = $this->find_by($method_name, $parameters); 521 } 522 # check for find_or_create_by_* magic functions 523 elseif(strlen($method_name) > 17 && substr($method_name, 0, 17) == "find_or_create_by") { 524 $result = $this->find_by($method_name, $parameters, "find_or_create"); 520 525 } 521 526 } … … 555 560 */ 556 561 private function find_all_habtm($other_table_name, $parameters = null) { 557 558 562 # Use any passed-in parameters 559 563 if (!is_null($parameters)) { … … 590 594 } 591 595 } 596 592 597 if(!is_null($other_object_name)) { 593 598 $other_class_name = Inflector::camelize($other_object_name); … … 595 600 $other_class_name = Inflector::classify($other_table_name); 596 601 } 602 597 603 # Instantiate an object to access find_all 598 604 $object = new $other_class_name(); … … 602 608 $join_table = $this->get_join_table_name($this->table_name, $other_table_name); 603 609 } 604 $this_foreign_key = Inflector::singularize($this->table_name)."_id"; 605 $other_foreign_key = Inflector::singularize($other_table_name)."_id"; 610 611 # Primary keys 612 $this_primary_key = $this->primary_keys[0]; 613 $other_primary_key = $object->primary_keys[0]; 614 615 # Foreign keys 616 $this_foreign_key = ($this_primary_key != 'id') ? $this_primary_key : Inflector::singularize($this->table_name)."_id"; 617 $other_foreign_key = ($other_primary_key != 'id') ? $other_primary_key : Inflector::singularize($other_table_name)."_id"; 618 619 # Primary key value 620 $this_primary_key_value = is_numeric($this->$this_primary_key) ? $this->$this_primary_key : "'".$this->$this_primary_key."'"; 621 606 622 # Set up the SQL segments 607 $conditions = "{$join_table}.{$this_foreign_key}=".intval($this->id); 608 $order = null; 609 $limit = null; 610 $joins .= "LEFT JOIN {$join_table} ON {$other_table_name}.id = {$other_foreign_key}"; 623 $conditions = "{$join_table}.{$this_foreign_key} = {$this_primary_key_value}"; 624 $joins .= "LEFT JOIN {$join_table} ON {$other_table_name}.{$other_primary_key} = {$join_table}.{$other_foreign_key}"; 611 625 612 626 # Get the list of other_class_name objects … … 973 987 * @uses find_first() 974 988 */ 975 private function find_by($method_name, $parameters, $find_all = false) { 976 $method_parts = explode("_",substr($method_name, ($find_all ? 12 : 8))); 977 if(is_array($method_parts)) { 978 $param_cnt = 0; 979 $part_cnt = 1; 980 $and_cnt = substr_count(strtolower($method_name), "_and_"); 981 $or_cnt = substr_count(strtolower($method_name), "_or_"); 982 $part_size = count($method_parts) - $and_cnt - $or_cnt; 983 // FIXME: This loop doesn't work right for either 984 // find_by_first_name_and_last_name or 985 // find_all_by_first_name_and_last_name 989 private function find_by($method_name, $parameters, $find_type = null) { 990 if($find_type == "find_or_create") { 991 $explode_len = 18; 992 } elseif($find_type == "all") { 993 $explode_len = 12; 994 } else { 995 $explode_len = 8; 996 } 997 $method_name = substr(strtolower($method_name), $explode_len); 998 $method_parts = explode("|", str_replace("_and_", "|AND|", $method_name)); 999 if(count($method_parts)) { 1000 $options = array(); 1001 $create_fields = array(); 1002 $param_index = 0; 986 1003 foreach($method_parts as $part) { 987 if(strtoupper($part) == "AND") { 988 $method_params .= implode("_",$field)."='".$parameters[$param_cnt++]."' AND "; 989 $part_cnt--; 990 unset($field); 991 } elseif(strtoupper($part) == "OR") { 992 $method_params .= implode("_",$field)."='".$parameters[$param_cnt++]."' OR "; 993 $part_cnt--; 994 unset($field); 1004 $value = is_numeric($parameters[$param_index]) ? $parameters[$param_index] : "'".$parameters[$param_index]."'"; 1005 if($part == "AND") { 1006 $conditions .= " AND "; 1007 $param_index++; 995 1008 } else { 996 $field[] = $part; 997 if($part_size == $part_cnt) { 998 $method_params .= implode("_",$field)."='".$parameters[$param_cnt++]."'"; 999 if($parameters[$param_cnt]) { 1000 $order = $parameters[$param_cnt]; 1001 } 1009 $create_fields[$part] = $parameters[$param_index]; 1010 $conditions .= "{$part} = {$value}"; 1011 } 1012 } 1013 # If last param exists and is a string set it as the ORDER BY clause 1014 # or if the last param is an array set it as the $options 1015 if($last_param = $parameters[++$param_index]) { 1016 if(is_string($last_param)) { 1017 $options['order'] = $last_param; 1018 } elseif(is_array($last_param)) { 1019 $options = $last_param; 1020 } 1021 } 1022 # Set the conditions 1023 if($options['conditions'] && $conditions) { 1024 $options['conditions'] = "(".$options['conditions'].") AND (".$conditions.")"; 1025 } else { 1026 $options['conditions'] = $conditions; 1027 } 1028 1029 # Now do the actual find with condtions from above 1030 if($find_type == "find_or_create") { 1031 # see if we can find a record with specified parameters 1032 $object = $this->find($options); 1033 if(is_object($object)) { 1034 # we found a record with the specified parameters so return it 1035 return $object; 1036 } elseif(count($create_fields)) { 1037 # can't find a record with specified parameters so create a new record 1038 # and return new object 1039 foreach($create_fields as $field => $value) { 1040 $this->$field = $value; 1002 1041 } 1042 $this->save(); 1043 return $this->find($options); 1003 1044 } 1004 $part_cnt++; 1005 } 1006 1007 if($find_all) { 1008 return $this->find_all($method_params, $order); 1045 } elseif($find_type == "all") { 1046 return $this->find_all($options); 1009 1047 } else { 1010 return $this->find _first($method_params, $order);1048 return $this->find($options); 1011 1049 } 1012 1050 } … … 1045 1083 * @throws {@link ActiveRecordError} 1046 1084 */ 1047 function find_all($conditions = null, $order = null, 1048 $limit = null, $joins = null) { 1085 function find_all($conditions = null, $order = null, $limit = null, $joins = null) { 1049 1086 //error_log("find_all(".(is_null($conditions)?'null':$conditions) 1050 1087 // .', ' . (is_null($order)?'null':$order) … … 1131 1168 # echo "ActiveRecord::find_all() - sql: $sql\n<br>"; 1132 1169 # echo "query: $sql\n"; 1170 # error_log("ActiveRecord::find_all -> $sql"); 1133 1171 if($this->is_error($rs = $this->query($sql))) { 1134 1172 $this->raise($rs->getMessage()); … … 1190 1228 function find($id, $order = null, $limit = null, $joins = null) { 1191 1229 if(is_array($id)) { 1192 $conditions = "id IN(".implode(",",$id).")"; 1193 } elseif(stristr($id,"=")) { # has an = so must be a where clause 1194 $conditions = $id; 1195 } else { 1196 $conditions = "id='$id'"; 1197 } 1198 1199 if(is_array($id)) { 1200 return $this->find_all($conditions, $order, $limit, $joins); 1201 } else { 1202 return $this->find_first($conditions, $order, $limit, $joins); 1230 if($id[0]) { 1231 # passed in array of numbers array(1,2,4,23) 1232 $primary_key = $this->primary_keys[0]; 1233 $primary_key_values = is_numeric($id[0]) ? implode(",", $id) : "'".implode("','", $id)."'"; 1234 $options['conditions'] = "{$primary_key} IN({$primary_key_values})"; 1235 $find_all = true; 1236 } else { 1237 # passed in an options array 1238 $options = $id; 1239 } 1240 } elseif(stristr($id, "=")) { 1241 # has an "=" so must be a WHERE clause 1242 $options['conditions'] = $id; 1243 } else { 1244 # find an single record with id = $id 1245 $primary_key = $this->primary_keys[0]; 1246 $primary_key_value = is_numeric($id) ? $id : "'".$id."'"; 1247 $options['conditions'] = "{$primary_key} = {$primary_key_value}"; 1248 } 1249 if(!is_null($order)) $options['order'] = $order; 1250 if(!is_null($limit)) $options['limit'] = $limit; 1251 if(!is_null($joins)) $options['joins'] = $joins; 1252 1253 1254 if($find_all) { 1255 return $this->find_all($options); 1256 } else { 1257 return $this->find_first($options); 1203 1258 } 1204 1259 } … … 1230 1285 */ 1231 1286 function find_first($conditions, $order = null, $limit = null, $joins = null) { 1232 $result = $this->find_all($conditions, $order, $limit, $joins); 1287 if(is_array($conditions)) { 1288 $options = $conditions; 1289 } else { 1290 $options['conditions'] = $conditions; 1291 } 1292 if(!is_null($order)) $options['order'] = $order; 1293 if(!is_null($limit)) $options['limit'] = $limit; 1294 if(!is_null($joins)) $options['joins'] = $joins; 1295 1296 $result = $this->find_all($options); 1233 1297 return @current($result); 1234 1298 } … … 1510 1574 $type = null; 1511 1575 if(is_string($this->has_many)) { 1512 if(preg_match("/ $association_name/", $this->has_many)) {1576 if(preg_match("/\b$association_name\b/", $this->has_many)) { 1513 1577 $type = "has_many"; 1514 1578 } … … 1519 1583 } 1520 1584 if(is_string($this->has_one)) { 1521 if(preg_match("/ $association_name/", $this->has_one)) {1585 if(preg_match("/\b$association_name/\b", $this->has_one)) { 1522 1586 $type = "has_one"; 1523 1587 } … … 1528 1592 } 1529 1593 if(is_string($this->belongs_to)) { 1530 if(preg_match("/ $association_name/", $this->belongs_to)) {1594 if(preg_match("/\b$association_name\b/", $this->belongs_to)) { 1531 1595 $type = "belongs_to"; 1532 1596 } … … 1537 1601 } 1538 1602 if(is_string($this->has_and_belongs_to_many)) { 1539 if(preg_match("/ $association_name/", $this->has_and_belongs_to_many)) {1603 if(preg_match("/\b$association_name\b/", $this->has_and_belongs_to_many)) { 1540 1604 $type = "has_and_belongs_to_many"; 1541 1605 }
