Show
Ignore:
Timestamp:
05/26/06 04:46:01 (6 years ago)
Author:
john
Message:

AR Bug fixes and fixed scaffold index template

Files:
1 modified

Legend:

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

    r201 r203  
    117117     */ 
    118118    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; 
    119129 
    120130    /** 
     
    134144     */ 
    135145    public $force_reconnect = false; # should we force a connection everytime 
     146     
    136147    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 
    137150 
    138151    # Table associations 
     
    316329    /** 
    317330     *  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 
    326338 
    327339    /** 
     
    643655        # customers has_many contacts, then we'll need a Contact 
    644656        # object, and the customer_id field name. 
    645         $other_class_name = Inflector::camelize($other_object_name); 
     657        $other_  = Inflector::camelize($other_object_name); 
    646658        if(@array_key_exists("foreign_key", $parameters)) 
    647659            $foreign_key = $parameters['foreign_key']; 
     
    800812     */ 
    801813    function send($column) { 
    802         if($this->column_attribute_exists($column)) { 
     814        if($this->column_attribute_exists($column) && ($conditions = $this->get_primary_key_conditions())) { 
    803815            # 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"; 
    805817            $this->log_query($sql); 
    806818            $result = self::$db->getOne($sql); 
     
    10501062        $objects = array(); 
    10511063        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); 
    10541066            $object = new $class(); 
    10551067            $object->new_record = false; 
     
    13291341     *  @uses is_error() 
    13301342     *  @uses query() 
    1331      *  @uses quoted_attributes() 
     1343     *  @uses get_inserts() 
    13321344     *  @uses raise() 
    13331345     *  @uses $table_name 
     
    13401352     */ 
    13411353    private function add_record() { 
    1342         $attributes = $this->quoted_attributes(); 
     1354        $attributes = $this->get_inserts(); 
    13431355        $fields = @implode(', ', array_keys($attributes)); 
    13441356        $values = @implode(', ', array_values($attributes)); 
     
    13491361            $this->raise($results->getMessage()); 
    13501362        } else { 
     1363            $habtm_result = true; 
    13511364            $this->id = $this->get_insert_id(); 
    13521365            if($this->id > 0) { 
     
    13911404            $this->raise($results->getMessage()); 
    13921405        } else { 
     1406            $habtm_result = true; 
    13931407            if($this->id > 0) { 
    13941408                if($this->auto_save_habtm) { 
     
    18511865        foreach ($attributes as $key => $value) { 
    18521866            $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)) { 
    18551871                $value = str_replace("\\\"","\"",$value); 
    18561872                $value = str_replace("\'","'",$value); 
     
    18631879        } 
    18641880        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; 
    18651901    } 
    18661902 
     
    18891925            foreach($attributes as $key => $value) { 
    18901926                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";     
    18961928                } 
    18971929            } 
     
    20012033     *  @uses $database_name 
    20022034     *  @uses $force_reconnect 
     2035     *  @uses Trax::$active_record_connections 
    20032036     *  @uses is_error() 
    20042037     *  @throws {@link ActiveRecordError} 
    20052038     */ 
    20062039    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                } 
    20112050            } 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. 
    20122055                $connection_settings = Trax::$database_settings[TRAX_ENV]; 
    20132056            } 
     
    20202063                $connection_options['persistent'] = $connection_settings['persistent']; 
    20212064            } 
    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; 
    20262071        } else { 
    2027             $this->raise(Trax::$active_record_connection->getMessage()); 
     2072            $this->raise($connection->getMessage()); 
    20282073        } 
    20292074        self::$db->setFetchMode($this->fetch_mode);