Changeset 106 for trunk/trax/vendor/trax/active_record.php
- Timestamp:
- 01/01/06 21:37:32 (6 years ago)
- Files:
-
- 1 modified
-
trunk/trax/vendor/trax/active_record.php (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/trax/vendor/trax/active_record.php
r94 r106 2 2 # $Id$ 3 3 # 4 # Copyright (c) 2005 John Peterson 4 # Copyright (c) 2005 John Peterson 5 5 # 6 6 # Permission is hereby granted, free of charge, to any person obtaining … … 30 30 static private $db = null; # Reference to Pear db object 31 31 static protected $inflector = null; # object to do class inflection 32 public $ table_info = null;# info about each column in the table32 public $content_columns = null; # info about each column in the table 33 33 public $table_name = null; 34 34 public $fetch_mode = DB_FETCHMODE_ASSOC; 35 35 36 36 # Table associations 37 protected $has_many = array();38 protected $has_one = array();39 protected $has_and_belongs_to_many = array();40 protected $belongs_to = array();41 protected $habtm_attributes = array();37 protected $has_many = null; 38 protected $has_one = null; 39 protected $has_and_belongs_to_many = null; 40 protected $belongs_to = null; 41 protected $habtm_attributes = null; 42 42 43 43 protected $new_record = true; # whether or not to create a new record or just update … … 49 49 public $rows_per_page_default = 20; # Pagination rows to display per page 50 50 public $display = 10; # Pagination how many numbers in the list << < 1 2 3 4 > >> 51 public $errors = array(); 51 public $errors = array(); 52 52 public $auto_timestamps = true; # whether or not to auto update created_at/on and updated_at/on fields 53 53 public $auto_save_habtm = true; # auto insert / update $has_and_belongs_to_many tables 54 54 55 55 # Transactions (only use if your db supports it) 56 private $begin_executed = false; # this is for transactions only to let query() know that a 'BEGIN' has been executed57 public $use_transactions = false; # this will issue a rollback command if any sql fails56 static private $begin_executed = false; # this is for transactions only to let query() know that a 'BEGIN' has been executed 57 static public $use_transactions = false; # this will issue a rollback command if any sql fails 58 58 59 59 # Constructor sets up need parameters for AR to function properly 60 60 function __construct($attributes = null) { 61 62 # Define static members63 if (self::$inflector == null) {64 self::$inflector = new Inflector();65 }66 67 61 # Open the database connection 68 62 $this->establish_connection(); 69 63 70 64 # Set $table_name 71 if($this->table_name == null) { 65 if($this->table_name == null) { 72 66 $this->set_table_name_using_class_name(); 73 67 } … … 75 69 # Set column info 76 70 if($this->table_name) { 77 $this->set_ table_info($this->table_name);71 $this->set_content_columns($this->table_name); 78 72 } 79 73 … … 130 124 //echo "setting: $key = $value<br>"; 131 125 if($key == "table_name") { 132 $this->set_ table_info($value);126 $this->set_content_columns($value); 133 127 } 134 128 $this->$key = $value; … … 223 217 # Returns: An array of ActiveRecord objects. (e.g. Movie objects) 224 218 function find_all_habtm($other_table_name, $parameters = null) { 225 $other_class_name = self::$inflector->classify($other_table_name);219 $other_class_name = Inflector::classify($other_table_name); 226 220 # Instantiate an object to access find_all 227 221 $results = new $other_class_name(); … … 229 223 # Prepare the join table name primary keys (fields) to do the join on 230 224 $join_table = $this->get_join_table_name($this->table_name, $other_table_name); 231 $this_foreign_key = self::$inflector->singularize($this->table_name)."_id";232 $other_foreign_key = self::$inflector->singularize($other_table_name)."_id";225 $this_foreign_key = Inflector::singularize($this->table_name)."_id"; 226 $other_foreign_key = Inflector::singularize($other_table_name)."_id"; 233 227 # Set up the SQL segments 234 228 $conditions = "`{$join_table}`.`{$this_foreign_key}`={$this->id}"; … … 281 275 $foreign_key = $parameters['foreign_key']; 282 276 else 283 $foreign_key = self::$inflector->singularize($this->table_name)."_id";284 285 $other_class_name = self::$inflector->classify($other_table_name);277 $foreign_key = Inflector::singularize($this->table_name)."_id"; 278 279 $other_class_name = Inflector::classify($other_table_name); 286 280 $conditions = "`{$foreign_key}`=$this->id"; 287 281 … … 332 326 # customers has_many contacts, then we'll need a Contact 333 327 # object, and the customer_id field name. 334 $other_class_name = self::$inflector->camelize($other_object_name);328 $other_class_name = Inflector::camelize($other_object_name); 335 329 if(@array_key_exists("foreign_key", $parameters)) 336 330 $foreign_key = $parameters['foreign_key']; 337 331 else 338 $foreign_key = self::$inflector->singularize($this->table_name)."_id";332 $foreign_key = Inflector::singularize($this->table_name)."_id"; 339 333 340 334 $conditions = "`$foreign_key`='{$this->id}'"; … … 361 355 # customers has_many contacts, then we'll need a Contact 362 356 # object, and the customer_id field name. 363 $other_class_name = self::$inflector->camelize($other_object_name);357 $other_class_name = Inflector::camelize($other_object_name); 364 358 if(@array_key_exists("foreign_key", $parameters)) 365 359 $foreign_key = $parameters['foreign_key']; … … 406 400 } 407 401 402 # Return if this is a new record or not. 403 function is_new_record() { 404 return $this->new_record; 405 } 406 407 # get the attributes for a specific column. 408 function column_for_attribute($attribute) { 409 if(is_array($this->content_columns)) { 410 foreach($this->content_columns as $column) { 411 if($column['name'] == $attribute) { 412 return $column; 413 } 414 } 415 } 416 return null; 417 } 418 408 419 # Returns PEAR result set of one record with only the passed in column in the result set. 409 420 function send($column) { … … 440 451 $rs = self::$db->query($sql); 441 452 if ($this->is_error($rs)) { 442 if( $this->use_transactions && $this->begin_executed) {453 if(self::$use_transactions && self::$begin_executed) { 443 454 $this->rollback(); 444 455 } … … 452 463 function find_all($conditions = null, $orderings = null, $limit = null, $joins = null) { 453 464 if (is_array($limit)) { 454 list( $this->page, $this->rows_per_page) = $limit;455 if( $this->page <= 0) $this->page = 1;465 list(self::$page, self::$rows_per_page) = $limit; 466 if(self::$$page <= 0) self::$page = 1; 456 467 # Default for rows_per_page: 457 if ( $this->rows_per_page == null) $this->rows_per_page = $this->rows_per_page_default;468 if (self::$rows_per_page == null) self::$rows_per_page = self::$rows_per_page_default; 458 469 # Set the LIMIT string segment for the SQL in the find_all 459 $this->offset = ($this->page - 1) * $this->rows_per_page;470 self::$offset = (self::$page - 1) * self::$rows_per_page; 460 471 # mysql 3.23 doesn't support OFFSET 461 472 //$limit = "$rows_per_page OFFSET $offset"; 462 $limit = "$this->offset, $this->rows_per_page";473 $limit = self::$offset.", ".self::$rows_per_page; 463 474 $set_pages = true; 464 475 } … … 467 478 $sql = $conditions; 468 479 } else { 469 $sql = "SELECT * FROM ` $this->table_name` ";480 $sql = "SELECT * FROM `".$this->table_name."` "; 470 481 if(!is_null($joins)) { 471 482 if(substr($joins,0,4) != "LEFT") $sql .= ","; … … 477 488 if($set_pages) { 478 489 //echo "ActiveRecord::find_all() - sql: $sql\n<br>"; 479 if( $this->is_error($rs = $this->query($sql))) {480 $this->raise($rs->getMessage());490 if(self::$is_error($rs = self::query($sql))) { 491 self::raise($rs->getMessage()); 481 492 } else { 482 493 # Set number of total pages in result set without the LIMIT 483 494 if($count = $rs->numRows()) 484 $this->pages = (($count % $this->rows_per_page) == 0) ? $count / $this->rows_per_page : floor($count / $this->rows_per_page) + 1;495 self::$pages = (($count % self::$rows_per_page) == 0) ? $count / self::$rows_per_page : floor($count / self::$rows_per_page) + 1; 485 496 } 486 497 } … … 490 501 491 502 //echo "ActiveRecord::find_all() - sql: $sql\n<br>"; 492 if( $this->is_error($rs = $this->query($sql))) {493 $this->raise($rs->getMessage());503 if(self::is_error($rs = self::query($sql))) { 504 self::raise($rs->getMessage()); 494 505 } 495 506 496 507 $objects = array(); 497 508 while($row = $rs->fetchRow()) { 498 $class = get_class($this); 509 #$class = get_class($this); 510 $class = Inflector::classify($this->table_name); 499 511 $object = new $class(); 500 512 $object->new_record = false; … … 525 537 526 538 if(is_array($id)) { 527 return $this->find_all($conditions, $orderings, $limit, $joins);539 return /*$this->*/find_all($conditions, $orderings, $limit, $joins); 528 540 } else { 529 541 return $this->find_first($conditions, $orderings, $limit, $joins); … … 777 789 foreach($this->habtm_attributes as $other_table_name => $other_foreign_values) { 778 790 $table_name = $this->get_join_table_name($this->table_name,$other_table_name); 779 $other_foreign_key = self::$inflector->singularize($other_table_name)."_id";780 $this_foreign_key = self::$inflector->singularize($this->table_name)."_id";791 $other_foreign_key = Inflector::singularize($other_table_name)."_id"; 792 $this_foreign_key = Inflector::singularize($this->table_name)."_id"; 781 793 foreach($other_foreign_values as $other_foreign_value) { 782 794 unset($attributes); … … 804 816 foreach($this->habtm_attributes as $other_table_name => $values) { 805 817 $table_name = $this->get_join_table_name($this->table_name,$other_table_name); 806 $this_foreign_key = self::$inflector->singularize($this->table_name)."_id";818 $this_foreign_key = Inflector::singularize($this->table_name)."_id"; 807 819 $sql = "DELETE FROM $table_name WHERE $this_foreign_key = $this_foreign_value"; 808 820 //echo "delete_habtm_records: SQL: $sql<br>"; … … 845 857 } 846 858 847 # If $this->set_ table_info() was previously called, which will mean that859 # If $this->set_content_columns() was previously called, which will mean that 848 860 # $table_info will be an array of containing the column info about the database 849 861 # table this model is representing. This will return an array where the keys … … 851 863 function get_attributes() { 852 864 $attributes = array(); 853 if(is_array($this-> table_info)) {854 foreach($this-> table_info as $info) {865 if(is_array($this->content_columns)) { 866 foreach($this->content_columns as $column) { 855 867 //echo "attribute: $info[name] -> {$this->$info[name]}<br>"; 856 $attributes[$ info['name']] = $this->$info['name'];868 $attributes[$column['name']] = $this->$column['name']; 857 869 } 858 870 } … … 927 939 # used in all queries throughout ActiveRecord 928 940 function set_table_name_using_class_name() { 929 if(! isset($this->table_name)) {930 $this->table_name = self::$inflector->tableize(get_class($this));941 if(!$this->table_name) { 942 $this->table_name = Inflector::tableize(get_class($this)); 931 943 } 932 944 } 933 945 934 946 # Populates the model object with information about the table it represents 935 function set_ table_info($table_name) {936 $this-> table_info= self::$db->tableInfo($table_name);937 if(is_array($this-> table_info)) {947 function set_content_columns($table_name) { 948 $this->content_columns = self::$db->tableInfo($table_name); 949 if(is_array($this->content_columns)) { 938 950 $i = 0; 939 foreach($this-> table_info as $info) {940 $this-> table_info[$i++]['human_name'] = self::$inflector->humanize($info['name']);951 foreach($this->content_columns as $column) { 952 $this->content_columns[$i++]['human_name'] = Inflector::humanize($column['name']); 941 953 } 942 954 } … … 989 1001 $error_message = "Model Class: ".get_class($this)."<br>"; 990 1002 $error_message .= "Error Message: ".$message; 991 throw new ActiveRecordError($error_message, "ActiveRecord Error", "500"); 1003 throw new ActiveRecordError($error_message, "ActiveRecord Error", "500"); 992 1004 } 993 1005
