Changeset 131 for trunk/trax/vendor/trax/active_record.php
- Timestamp:
- 02/13/06 12:15:03 (6 years ago)
- Files:
-
- 1 modified
-
trunk/trax/vendor/trax/active_record.php (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/trax/vendor/trax/active_record.php
r128 r131 83 83 # contents from the database. 84 84 function __get($key) { 85 86 85 if(is_string($this->has_many)) { 87 86 if(preg_match("/$key/", $this->has_many)) { … … 92 91 $this->$key = $this->find_all_has_many($key, $this->has_many[$key]); 93 92 } 94 } elseif(is_string($this->has_one)) { 93 } 94 if(is_string($this->has_one)) { 95 95 if(preg_match("/$key/", $this->has_one)) { 96 96 $this->$key = $this->find_one_has_one($key); … … 100 100 $this->$key = $this->find_one_has_one($key, $this->has_one[$key]); 101 101 } 102 } elseif(is_string($this->belongs_to)) { 102 } 103 if(is_string($this->belongs_to)) { 103 104 if(preg_match("/$key/", $this->belongs_to)) { 104 105 $this->$key = $this->find_one_belongs_to($key); … … 108 109 $this->$key = $this->find_one_belongs_to($key, $this->belongs_to[$key]); 109 110 } 110 } elseif(is_string($this->has_and_belongs_to_many)) { 111 } 112 if(is_string($this->has_and_belongs_to_many)) { 111 113 if(preg_match("/$key/", $this->has_and_belongs_to_many)) { 112 114 $this->$key = $this->find_all_habtm($key); … … 149 151 $result = $this->find_all_has_many($method_name, $parameters); 150 152 } 151 } elseif(is_string($this->has_one)) { 153 } 154 if(is_string($this->has_one)) { 152 155 if(preg_match("/$method_name/", $this->has_one)) { 153 156 $result = $this->find_one_has_one($method_name, $parameters); … … 157 160 $result = $this->find_one_has_one($method_name, $parameters); 158 161 } 159 } elseif(is_string($this->belongs_to)) { 162 } 163 if(is_string($this->belongs_to)) { 160 164 if(preg_match("/$method_name/", $this->belongs_to)) { 161 165 $result = $this->find_one_belongs_to($method_name, $parameters); … … 165 169 $result = $this->find_one_belongs_to($method_name, $parameters); 166 170 } 167 } elseif(is_string($this->has_and_belongs_to_many)) { 171 } 172 if(is_string($this->has_and_belongs_to_many)) { 168 173 if(preg_match("/$method_name/", $this->has_and_belongs_to_many)) { 169 174 $result = $this->find_all_habtm($method_name, $parameters); … … 175 180 } 176 181 # 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)) { 178 183 //echo "calling method: $method_name<br>"; 179 184 $result = $this->aggregrate_all($method_name, $parameters); … … 214 219 # 215 220 # 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 217 222 # $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 219 224 # Returns: An array of ActiveRecord objects. (e.g. Movie objects) 220 225 function find_all_habtm($other_table_name, $parameters = null) { … … 228 233 $other_foreign_key = Inflector::singularize($other_table_name)."_id"; 229 234 # Set up the SQL segments 230 $conditions = " `{$join_table}`.`{$this_foreign_key}`={$this->id}";235 $conditions = "{$join_table}.{$this_foreign_key}={$this->id}"; 231 236 $orderings = null; 232 237 $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}"; 234 239 235 240 # Use any passed-in parameters … … 280 285 281 286 $other_class_name = Inflector::classify($other_table_name); 282 $conditions = " `{$foreign_key}`=$this->id";287 $conditions = "{$foreign_key}=$this->id"; 283 288 284 289 # Use any passed-in parameters … … 334 339 $foreign_key = Inflector::singularize($this->table_name)."_id"; 335 340 336 $conditions = " `$foreign_key`='{$this->id}'";341 $conditions = "$foreign_key='{$this->id}'"; 337 342 # Instantiate an object to access find_all 338 343 $results = new $other_class_name(); … … 381 386 $aggregrate_type = strtoupper(substr($aggregrate_type, 0, -4)); 382 387 ($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 "; 384 389 385 390 # Use any passed-in parameters … … 389 394 } 390 395 391 if(!empty($joins)) $sql .= ", `$joins`";396 if(!empty($joins)) $sql .= ",$joins "; 392 397 if(!empty($conditions)) $sql .= "WHERE $conditions "; 393 398 … … 418 423 return null; 419 424 } 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 } 420 437 421 438 # Returns PEAR result set of one record with only the passed in column in the result set. 422 439 function send($column) { 423 if($ column != "") {440 if($this->column_attribute_exists($column)) { 424 441 # 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'"); 426 443 if($this->is_error($result)) { 427 444 $this->raise($result->getMessage()); … … 480 497 $sql = $conditions; 481 498 } else { 482 $sql = "SELECT * FROM `".$this->table_name."`";499 $sql = "SELECT * FROM ".$this->table_name." "; 483 500 if(!is_null($joins)) { 484 501 if(substr($joins,0,4) != "LEFT") $sql .= ","; … … 646 663 # $model->update_all("category = 'cooldude', approved = 1", "author = 'John'"); 647 664 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"; 649 666 $result = $this->query($sql); 650 667 if ($this->is_error($result)) { … … 695 712 $fields = @implode(', ', array_keys($attributes)); 696 713 $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)"; 698 715 //echo "add_record: SQL: $sql<br>"; 699 716 … … 715 732 $updates = $this->get_updates_sql(); 716 733 $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"; 718 735 //echo "update_record: SQL: $sql<br>"; 719 736 $result = $this->query($sql); … … 759 776 760 777 # 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"))) { 762 779 $this->raise($rs->getMessage()); 763 780 } … … 918 935 $return[$key] = $value; 919 936 } 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); 923 938 } 924 939 return $return; … … 937 952 # run through our fields and join them with their values 938 953 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 } 941 960 } 942 961 }
