Show
Ignore:
Timestamp:
02/13/06 12:15:03 (6 years ago)
Author:
john
Message:

fixed a bunch of helpers

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/trax/vendor/trax/active_record.php

    r128 r131  
    8383    # contents from the database. 
    8484    function __get($key) { 
    85  
    8685        if(is_string($this->has_many)) { 
    8786            if(preg_match("/$key/", $this->has_many)) { 
     
    9291                $this->$key = $this->find_all_has_many($key, $this->has_many[$key]); 
    9392            } 
    94         } elseif(is_string($this->has_one)) { 
     93        } 
     94        if(is_string($this->has_one)) { 
    9595            if(preg_match("/$key/", $this->has_one)) { 
    9696                $this->$key = $this->find_one_has_one($key); 
     
    100100                $this->$key = $this->find_one_has_one($key, $this->has_one[$key]); 
    101101            } 
    102         } elseif(is_string($this->belongs_to)) { 
     102        } 
     103        if(is_string($this->belongs_to)) {  
    103104            if(preg_match("/$key/", $this->belongs_to)) { 
    104105                $this->$key = $this->find_one_belongs_to($key); 
     
    108109                $this->$key = $this->find_one_belongs_to($key, $this->belongs_to[$key]); 
    109110            } 
    110         } elseif(is_string($this->has_and_belongs_to_many)) { 
     111        } 
     112        if(is_string($this->has_and_belongs_to_many)) { 
    111113            if(preg_match("/$key/", $this->has_and_belongs_to_many)) { 
    112114                $this->$key = $this->find_all_habtm($key); 
     
    149151                    $result = $this->find_all_has_many($method_name, $parameters); 
    150152                } 
    151             } elseif(is_string($this->has_one)) { 
     153            }  
     154            if(is_string($this->has_one)) { 
    152155                if(preg_match("/$method_name/", $this->has_one)) { 
    153156                    $result = $this->find_one_has_one($method_name, $parameters); 
     
    157160                    $result = $this->find_one_has_one($method_name, $parameters); 
    158161                } 
    159             } elseif(is_string($this->belongs_to)) { 
     162            }  
     163            if(is_string($this->belongs_to)) { 
    160164                if(preg_match("/$method_name/", $this->belongs_to)) { 
    161165                    $result = $this->find_one_belongs_to($method_name, $parameters); 
     
    165169                    $result = $this->find_one_belongs_to($method_name, $parameters); 
    166170                } 
    167             } elseif(is_string($this->has_and_belongs_to_many)) { 
     171            }  
     172            if(is_string($this->has_and_belongs_to_many)) { 
    168173                if(preg_match("/$method_name/", $this->has_and_belongs_to_many)) { 
    169174                    $result = $this->find_all_habtm($method_name, $parameters); 
     
    175180            } 
    176181            # check for the [count,sum,avg,etc...]_all magic functions 
    177             elseif(substr($method_name, -4) == "_all" && in_array(substr($method_name, 0, -4),$this->aggregrations)) { 
     182            if(substr($method_name, -4) == "_all" && in_array(substr($method_name, 0, -4),$this->aggregrations)) { 
    178183                //echo "calling method: $method_name<br>"; 
    179184                $result = $this->aggregrate_all($method_name, $parameters); 
     
    214219    # 
    215220    # Parameters: $this_table_name:  The name of the database table that has the 
    216     #                                one row you are interested in.  E.g. `genres` 
     221    #                                one row you are interested in.  E.g. genres 
    217222    #             $other_table_name: The name of the database table that has the 
    218     #                                many rows you are interested in.  E.g. `movies` 
     223    #                                many rows you are interested in.  E.g. movies 
    219224    # Returns: An array of ActiveRecord objects. (e.g. Movie objects) 
    220225    function find_all_habtm($other_table_name, $parameters = null) { 
     
    228233        $other_foreign_key = Inflector::singularize($other_table_name)."_id"; 
    229234        # Set up the SQL segments 
    230         $conditions = "`{$join_table}`.`{$this_foreign_key}`={$this->id}"; 
     235        $conditions = "{$join_table}.{$this_foreign_key}={$this->id}"; 
    231236        $orderings = null; 
    232237        $limit = null; 
    233         $joins = "LEFT JOIN `{$join_table}` ON `{$other_table_name}`.id = `{$other_foreign_key}`"; 
     238        $joins = "LEFT JOIN {$join_table} ON {$other_table_name}.id = {$other_foreign_key}"; 
    234239 
    235240        # Use any passed-in parameters 
     
    280285 
    281286        $other_class_name = Inflector::classify($other_table_name); 
    282         $conditions = "`{$foreign_key}`=$this->id"; 
     287        $conditions = "{$foreign_key}=$this->id"; 
    283288 
    284289        # Use any passed-in parameters 
     
    334339            $foreign_key = Inflector::singularize($this->table_name)."_id"; 
    335340 
    336         $conditions = "`$foreign_key`='{$this->id}'"; 
     341        $conditions = "$foreign_key='{$this->id}'"; 
    337342        # Instantiate an object to access find_all 
    338343        $results = new $other_class_name(); 
     
    381386        $aggregrate_type = strtoupper(substr($aggregrate_type, 0, -4)); 
    382387        ($parameters[0]) ? $field = $parameters[0] : $field = "*"; 
    383         $sql = "SELECT $aggregrate_type($field) AS agg_result FROM `$this->table_name` "; 
     388        $sql = "SELECT $aggregrate_type($field) AS agg_result FROM $this->table_name "; 
    384389 
    385390        # Use any passed-in parameters 
     
    389394        } 
    390395 
    391         if(!empty($joins)) $sql .= ",`$joins` "; 
     396        if(!empty($joins)) $sql .= ",$joins "; 
    392397        if(!empty($conditions)) $sql .= "WHERE $conditions "; 
    393398 
     
    418423        return null; 
    419424    } 
     425     
     426    # checks if a column exists or not in the table 
     427    function column_attribute_exists($attribute) { 
     428        if(is_array($this->content_columns)) { 
     429            foreach($this->content_columns as $column) { 
     430                if($column['name'] == $attribute) { 
     431                    return true; 
     432                } 
     433            } 
     434        }  
     435        return false;      
     436    } 
    420437 
    421438    # Returns PEAR result set of one record with only the passed in column in the result set. 
    422439    function send($column) { 
    423         if($column != "") { 
     440        if($this->column_attribute_exists($column)) { 
    424441            # Run the query to grab a specific columns value. 
    425             $result = self::$db->getOne("SELECT `$column` FROM `$this->table_name` WHERE id='$this->id'"); 
     442            $result = self::$db->getOne("SELECT $column FROM $this->table_name WHERE id='$this->id'"); 
    426443            if($this->is_error($result)) { 
    427444                $this->raise($result->getMessage()); 
     
    480497            $sql = $conditions; 
    481498        } else { 
    482             $sql  = "SELECT * FROM `".$this->table_name."` "; 
     499            $sql  = "SELECT * FROM ".$this->table_name." "; 
    483500            if(!is_null($joins)) { 
    484501                if(substr($joins,0,4) != "LEFT") $sql .= ","; 
     
    646663    #   $model->update_all("category = 'cooldude', approved = 1", "author = 'John'"); 
    647664    function update_all($updates, $conditions = null) { 
    648         $sql = "UPDATE `$this->table_name` SET $updates WHERE $conditions"; 
     665        $sql = "UPDATE $this->table_name SET $updates WHERE $conditions"; 
    649666        $result = $this->query($sql); 
    650667        if ($this->is_error($result)) { 
     
    695712        $fields = @implode(', ', array_keys($attributes)); 
    696713        $values = @implode(', ', array_values($attributes)); 
    697         $sql = "INSERT INTO `$this->table_name` ($fields) VALUES ($values)"; 
     714        $sql = "INSERT INTO $this->table_name ($fields) VALUES ($values)"; 
    698715        //echo "add_record: SQL: $sql<br>"; 
    699716 
     
    715732        $updates = $this->get_updates_sql(); 
    716733        $conditions = $this->get_primary_key_conditions(); 
    717         $sql = "UPDATE `$this->table_name` SET $updates WHERE $conditions"; 
     734        $sql = "UPDATE $this->table_name SET $updates WHERE $conditions"; 
    718735        //echo "update_record: SQL: $sql<br>"; 
    719736        $result = $this->query($sql); 
     
    759776 
    760777        # Delete the record(s) 
    761         if($this->is_error($rs = $this->query("DELETE FROM `$this->table_name` WHERE $conditions"))) { 
     778        if($this->is_error($rs = $this->query("DELETE FROM $this->table_name WHERE $conditions"))) { 
    762779            $this->raise($rs->getMessage()); 
    763780        } 
     
    918935                $return[$key] = $value; 
    919936            } 
    920             # quoteSmart is quoting the primary key need to fix this 
    921             # for now just use the above to quote fields 
    922             # $return[$key] = self::$db->quoteSmart($value); 
     937            //$return[$key] = self::$db->quoteSmart($value); 
    923938        } 
    924939        return $return; 
     
    937952            # run through our fields and join them with their values 
    938953            foreach($attributes as $key => $value) { 
    939                 if(in_array($key,$this->primary_keys)) { 
    940                     $conditions[] = "$key = $value"; 
     954                if(in_array($key, $this->primary_keys)) { 
     955                    if(!is_numeric($value) && !strstr($value, "'")) { 
     956                        $conditions[] = "$key = '$value'"; 
     957                    } else { 
     958                        $conditions[] = "$key = $value";     
     959                    } 
    941960                } 
    942961            }