Changeset 223 for trunk/trax/vendor/trax

Show
Ignore:
Timestamp:
06/21/06 04:16:01 (6 years ago)
Author:
john
Message:

fixed AR associations and find_by_* magic functions added find_or_create_by_* fixed url_helper url_for other AR bug fixes

Location:
trunk/trax/vendor/trax
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/trax/vendor/trax/action_view/helpers/url_helper.php

    r207 r223  
    280280            } 
    281281             
    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            } 
    289289        } 
    290290         
  • trunk/trax/vendor/trax/active_record.php

    r215 r223  
    407407            return null; 
    408408        } 
     409        //error_log("association_type:$association_type"); 
    409410        switch($association_type) { 
    410411            case "has_many": 
     
    512513            elseif(strlen($method_name) > 11 && substr($method_name, 0, 11) == "find_all_by") { 
    513514                //echo "calling method: $method_name<br>"; 
    514                 $result = $this->find_by($method_name, $parameters, true); 
     515                $result = $this->find_by($method_name, $parameters, "all"); 
    515516            } 
    516517            # check for the find_by_* magic functions 
     
    518519                //echo "calling method: $method_name<br>"; 
    519520                $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");         
    520525            } 
    521526        } 
     
    555560     */ 
    556561    private function find_all_habtm($other_table_name, $parameters = null) { 
    557  
    558562        # Use any passed-in parameters 
    559563        if (!is_null($parameters)) { 
     
    590594            }  
    591595        } 
     596         
    592597        if(!is_null($other_object_name)) { 
    593598            $other_class_name = Inflector::camelize($other_object_name);     
     
    595600            $other_class_name = Inflector::classify($other_table_name); 
    596601        } 
     602         
    597603        # Instantiate an object to access find_all 
    598604        $object = new $other_class_name(); 
     
    602608            $join_table = $this->get_join_table_name($this->table_name, $other_table_name); 
    603609        } 
    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 
    606622        # 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}"; 
    611625 
    612626        # Get the list of other_class_name objects 
     
    973987     *  @uses find_first() 
    974988     */ 
    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; 
    9861003            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++; 
    9951008                } 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;     
    10021041                    } 
     1042                    $this->save(); 
     1043                    return $this->find($options); 
    10031044                } 
    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); 
    10091047            } else { 
    1010                 return $this->find_first($method_params, $order); 
     1048                return $this->find($options); 
    10111049            } 
    10121050        } 
     
    10451083     *  @throws {@link ActiveRecordError} 
    10461084     */ 
    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) { 
    10491086        //error_log("find_all(".(is_null($conditions)?'null':$conditions) 
    10501087        //          .', ' . (is_null($order)?'null':$order) 
     
    11311168        # echo "ActiveRecord::find_all() - sql: $sql\n<br>"; 
    11321169        # echo "query: $sql\n"; 
     1170        # error_log("ActiveRecord::find_all -> $sql"); 
    11331171        if($this->is_error($rs = $this->query($sql))) { 
    11341172            $this->raise($rs->getMessage()); 
     
    11901228    function find($id, $order = null, $limit = null, $joins = null) { 
    11911229        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); 
    12031258        } 
    12041259    } 
     
    12301285     */ 
    12311286    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); 
    12331297        return @current($result); 
    12341298    } 
     
    15101574        $type = null; 
    15111575        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)) { 
    15131577                $type = "has_many";     
    15141578            } 
     
    15191583        } 
    15201584        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)) { 
    15221586                $type = "has_one";      
    15231587            } 
     
    15281592        } 
    15291593        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)) { 
    15311595                $type = "belongs_to";       
    15321596            } 
     
    15371601        } 
    15381602        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)) { 
    15401604                $type = "has_and_belongs_to_many";       
    15411605            }