Changeset 203 for trunk/trax/vendor/trax/active_record.php
- Timestamp:
- 05/26/06 04:46:01 (6 years ago)
- Files:
-
- 1 modified
-
trunk/trax/vendor/trax/active_record.php (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/trax/vendor/trax/active_record.php
r201 r203 117 117 */ 118 118 public $database_name = null; 119 120 /** 121 * Index into the Trax::$active_record_connections array 122 * 123 * Name of the index to use to return or set the current db connection 124 * Mainly used if you want to connect to different databases between 125 * different models. 126 * @var string 127 */ 128 public $connection_name = TRAX_ENV; 119 129 120 130 /** … … 134 144 */ 135 145 public $force_reconnect = false; # should we force a connection everytime 146 136 147 public $index_on = "id"; # find_all returns an array of objects each object index is off of this field 148 149 public $lock_optimistically = true; # page 222 Rails books 137 150 138 151 # Table associations … … 316 329 /** 317 330 * Transactions (only use if your db supports it) 318 * <b>FIXME: static should be after private</b> 319 */ 320 static private $begin_executed = false; # this is for transactions only to let query() know that a 'BEGIN' has been executed 321 322 /** 323 * <b>FIXME: static should be after public</b> 324 */ 325 static public $use_transactions = false; # this will issue a rollback command if any sql fails 331 */ 332 private static $begin_executed = false; # this is for transactions only to let query() know that a 'BEGIN' has been executed 333 334 /** 335 * Transactions (only use if your db supports it) 336 */ 337 public static $use_transactions = false; # this will issue a rollback command if any sql fails 326 338 327 339 /** … … 643 655 # customers has_many contacts, then we'll need a Contact 644 656 # object, and the customer_id field name. 645 $other_ class_name= Inflector::camelize($other_object_name);657 $other_ = Inflector::camelize($other_object_name); 646 658 if(@array_key_exists("foreign_key", $parameters)) 647 659 $foreign_key = $parameters['foreign_key']; … … 800 812 */ 801 813 function send($column) { 802 if($this->column_attribute_exists($column) ) {814 if($this->column_attribute_exists($column) && ($conditions = $this->get_primary_key_conditions())) { 803 815 # Run the query to grab a specific columns value. 804 $sql = "SELECT $column FROM $this->table_name WHERE id='$this->id'";816 $sql = "SELECT $column FROM $this->table_name WHERE $conditions"; 805 817 $this->log_query($sql); 806 818 $result = self::$db->getOne($sql); … … 1050 1062 $objects = array(); 1051 1063 while($row = $rs->fetchRow()) { 1052 #$class = get_class($this);1053 $class = Inflector::classify($this->table_name);1064 $class = get_class($this); 1065 #$class = Inflector::classify($this->table_name); 1054 1066 $object = new $class(); 1055 1067 $object->new_record = false; … … 1329 1341 * @uses is_error() 1330 1342 * @uses query() 1331 * @uses quoted_attributes()1343 * @uses get_inserts() 1332 1344 * @uses raise() 1333 1345 * @uses $table_name … … 1340 1352 */ 1341 1353 private function add_record() { 1342 $attributes = $this-> quoted_attributes();1354 $attributes = $this->get_inserts(); 1343 1355 $fields = @implode(', ', array_keys($attributes)); 1344 1356 $values = @implode(', ', array_values($attributes)); … … 1349 1361 $this->raise($results->getMessage()); 1350 1362 } else { 1363 $habtm_result = true; 1351 1364 $this->id = $this->get_insert_id(); 1352 1365 if($this->id > 0) { … … 1391 1404 $this->raise($results->getMessage()); 1392 1405 } else { 1406 $habtm_result = true; 1393 1407 if($this->id > 0) { 1394 1408 if($this->auto_save_habtm) { … … 1851 1865 foreach ($attributes as $key => $value) { 1852 1866 $value = $this->check_datetime($key, $value); 1853 # If the value isn't a function or null quote it. 1854 if(!(preg_match('/^\w+\(.*\)$/U', $value)) && !(strcasecmp($value, 'NULL') == 0)) { 1867 # If the value isn't a function, null, or numeric quote it. 1868 if(!(preg_match('/^\w+\(.*\)$/U', $value)) 1869 && !(strcasecmp($value, 'NULL') == 0) 1870 && !is_numeric($value)) { 1855 1871 $value = str_replace("\\\"","\"",$value); 1856 1872 $value = str_replace("\'","'",$value); … … 1863 1879 } 1864 1880 return $return; 1881 } 1882 1883 /** 1884 * Return column values for SQL insert statement 1885 * 1886 * Return an array containing the column names and values of this 1887 * object, filtering out the primary keys, which are not set. 1888 * 1889 * @uses $primary_keys 1890 * @uses quoted_attributes() 1891 */ 1892 function get_inserts() { 1893 $attributes = $this->quoted_attributes(); 1894 $inserts = array(); 1895 foreach($attributes as $key => $value) { 1896 if(!in_array($key, $this->primary_keys) || ($value != "''")) { 1897 $inserts[$key] = $value; 1898 } 1899 } 1900 return $inserts; 1865 1901 } 1866 1902 … … 1889 1925 foreach($attributes as $key => $value) { 1890 1926 if(in_array($key, $this->primary_keys)) { 1891 if(!is_numeric($value) && !strstr($value, "'")) { 1892 $conditions[] = "$key = '$value'"; 1893 } else { 1894 $conditions[] = "$key = $value"; 1895 } 1927 $conditions[] = "$key = $value"; 1896 1928 } 1897 1929 } … … 2001 2033 * @uses $database_name 2002 2034 * @uses $force_reconnect 2035 * @uses Trax::$active_record_connections 2003 2036 * @uses is_error() 2004 2037 * @throws {@link ActiveRecordError} 2005 2038 */ 2006 2039 function establish_connection() { 2007 # Connect to the database and throw an error if the connect fails. 2008 if(!is_object(Trax::$active_record_connection) || $this->force_reconnect) { 2009 if(array_key_exists("use", Trax::$database_settings[TRAX_ENV])) { 2010 $connection_settings = Trax::$database_settings[Trax::$database_settings[TRAX_ENV]['use']]; 2040 $connection = Trax::$active_record_connections[$this->connection_name]; 2041 if(!is_object($connection) || $this->force_reconnect) { 2042 if(array_key_exists($this->connection_name, Trax::$database_settings)) { 2043 # Use a different custom sections settings ? 2044 if(array_key_exists("use", Trax::$database_settings[$this->connection_name])) { 2045 $connection_settings = Trax::$database_settings[Trax::$database_settings[$this->connection_name]['use']]; 2046 } else { 2047 # Custom defined db settings in database.ini 2048 $connection_settings = Trax::$database_settings[$this->connection_name]; 2049 } 2011 2050 } else { 2051 # Just use the current TRAX_ENV's environment db settings 2052 # $this->connection_name's default value is TRAX_ENV so 2053 # if should never really get here unless override $this->connection_name 2054 # and you define a custom db section in database.ini and it can't find it. 2012 2055 $connection_settings = Trax::$database_settings[TRAX_ENV]; 2013 2056 } … … 2020 2063 $connection_options['persistent'] = $connection_settings['persistent']; 2021 2064 } 2022 Trax::$active_record_connection =& DB::Connect($connection_settings, $connection_options); 2023 } 2024 if(!$this->is_error(Trax::$active_record_connection)) { 2025 self::$db = Trax::$active_record_connection; 2065 # Connect to the database and throw an error if the connect fails. 2066 $connection =& DB::Connect($connection_settings, $connection_options); 2067 } 2068 if(!$this->is_error($connection)) { 2069 Trax::$active_record_connections[$this->connection_name] =& $connection; 2070 self::$db =& $connection; 2026 2071 } else { 2027 $this->raise( Trax::$active_record_connection->getMessage());2072 $this->raise($connection->getMessage()); 2028 2073 } 2029 2074 self::$db->setFetchMode($this->fetch_mode);
