Changeset 249 for trunk/trax/vendor/trax

Show
Ignore:
Timestamp:
08/24/06 13:18:40 (6 years ago)
Author:
john
Message:

fixed some logic errors with determining field types

Location:
trunk/trax/vendor/trax
Files:
2 modified

Legend:

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

    r247 r249  
    370370     */ 
    371371    public static $use_transactions = false; # this will issue a rollback command if any sql fails 
     372     
     373    /** 
     374     *  Keep a log of queries executed if in development env 
     375     */     
     376    public static $query_log = array(); 
    372377 
    373378    /** 
     
    635640             
    636641            # Primary key value 
    637             $this_primary_key_value = $this->attribute_is_string($this_primary_key) ?  
    638                 "'".$this->$this_primary_key."'" :  
    639                 $this->$this_primary_key; 
     642            if($this->attribute_is_string($this_primary_key)) { 
     643                $this_primary_key_value = "'".$this->$this_primary_key."'";                     
     644            } elseif(is_numeric($this->$this_primary_key)) { 
     645                $this_primary_key_value = $this->$this_primary_key; 
     646            } else { 
     647                $this_primary_key_value = 0; 
     648            } 
    640649 
    641650            # Set up the SQL segments 
     
    712721             
    713722            $foreign_key_value = $this->$this_primary_key; 
    714             $conditions = ($other_class_object->attribute_is_string($foreign_key) ?  
    715                 "$foreign_key = '{$foreign_key_value}'" :  
    716                 "$foreign_key = {$foreign_key_value}") . $additional_conditions;  
     723            if($other_class_object->attribute_is_string($foreign_key)) { 
     724                $conditions = "{$foreign_key} = '{$foreign_key_value}'";                     
     725            } elseif(is_numeric($foreign_key_value)) { 
     726                $conditions = "{$foreign_key} = {$foreign_key_value}"; 
     727            } else { 
     728                $conditions = "{$foreign_key} = 0"; 
     729            }             
     730            $conditions .= $additional_conditions;  
    717731        } 
    718732                          
     
    765779 
    766780        $foreign_key_value = $this->$this_primary_key; 
    767         $conditions = ($other_class_object->attribute_is_string($foreign_key) ?  
    768             "{$foreign_key} = '{$foreign_key_value}'" :  
    769             "{$foreign_key} = {$foreign_key_value}") . $additional_conditions;  
     781        if($other_class_object->attribute_is_string($foreign_key)) { 
     782            $conditions = "{$foreign_key} = '{$foreign_key_value}'";                     
     783        } elseif(is_numeric($foreign_key_value)) { 
     784            $conditions = "{$foreign_key} = {$foreign_key_value}"; 
     785        } else { 
     786            $conditions = "{$foreign_key} = 0"; 
     787        } 
     788 
     789        $conditions .= $additional_conditions;  
    770790         
    771791        # Get the list of other_class_name objects 
     
    822842         
    823843        $other_primary_key_value = $this->$foreign_key; 
    824         $conditions = ($other_class_object->attribute_is_string($other_primary_key) ?  
    825             "{$other_primary_key} = '{$other_primary_key_value}'" :  
    826             "{$other_primary_key} = {$other_primary_key_value}") . $additional_conditions; 
     844        if($other_class_object->attribute_is_string($other_primary_key)) { 
     845            $conditions = "{$other_primary_key} = '{$other_primary_key_value}'";                     
     846        } elseif(is_numeric($other_primary_key_value)) { 
     847            $conditions = "{$other_primary_key} = {$other_primary_key_value}"; 
     848        } else { 
     849            $conditions = "{$other_primary_key} = 0"; 
     850        } 
     851        $conditions .= $additional_conditions; 
    827852         
    828853        # Get the list of other_class_name objects 
     
    962987        if($this->column_attribute_exists($column) && ($conditions = $this->get_primary_key_conditions())) { 
    963988            # Run the query to grab a specific columns value. 
    964             $sql = "SELECT $column FROM $this->table_name WHERE $conditions"; 
     989            $sql = "SELECT {$column} FROM {$this->table_name} WHERE {$conditions}"; 
    965990            $this->log_query($sql); 
    966991            $result = self::$db->getOne($sql); 
     
    20952120               && !(strcasecmp($value, 'NULL') == 0)  
    20962121               && $this->attribute_is_string($key)) { 
     2122               //&& !is_numeric($value)) { 
    20972123                $value = str_replace("\\\"","\"", $value); 
    20982124                $value = str_replace("\'","'", $value); 
     
    21202146        $inserts = array(); 
    21212147        foreach($attributes as $key => $value) { 
    2122             if(!in_array($key, $this->primary_keys) || ($value != "''")) { 
     2148            if(!in_array($key, $this->primary_keys) || ($value != "''" && isset($value))) { 
    21232149                $inserts[$key] = $value; 
    21242150            } 
     
    21502176            # run through our fields and join them with their values 
    21512177            foreach($attributes as $key => $value) { 
    2152                 if(in_array($key, $this->primary_keys)) { 
     2178                if(in_array($key, $this->primary_keys) && isset($value) && $value != "''") { 
    21532179                    $conditions[] = "$key = $value";     
    21542180                } 
     
    23562382     */ 
    23572383    function add_error($error, $key = null) { 
    2358         if(!is_null($key))  
     2384        if(!is_null($key)) { 
    23592385            $this->errors[$key] = $error; 
    2360         else 
     2386        } else { 
    23612387            $this->errors[] = $error; 
     2388        } 
    23622389    } 
    23632390 
     
    26282655     *  Log SQL query in development mode 
    26292656     * 
    2630      *  If running in development mode, log the query to $GLOBAL 
     2657     *  If running in development mode, log the query to self::$query_log 
    26312658     *  @param string SQL to be logged 
    26322659     */ 
    2633     function log_query($sql) { 
    2634         if(TRAX_ENV == "development" && $sql) { 
    2635             $GLOBALS['ACTIVE_RECORD_SQL_LOG'][] = $sql;        
     2660    function log_query($query) { 
     2661        if(TRAX_ENV == "development" && $query) { 
     2662            self::$query_log[] = $query;        
    26362663        }     
    26372664    } 
  • trunk/trax/vendor/trax/templates/error.phtml

    r192 r249  
    4949<?php endif; ?> 
    5050 
    51 <?php if(array_key_exists('ACTIVE_RECORD_SQL_LOG',$GLOBALS) 
    52      && count($GLOBALS['ACTIVE_RECORD_SQL_LOG'])): ?> 
     51<?php if(count(ActiveRecord::$query_log)): ?> 
    5352<p><a href="#" onclick="expandContract('sql_trace')">Show SQL trace</a> 
    5453<pre id="sql_trace" style="display:none"> 
    5554<code style="word-wrap: break-word;"> 
    5655<?php $i = 0 ?>     
    57 <?php foreach(array_reverse($GLOBALS['ACTIVE_RECORD_SQL_LOG']) as $sql): ?> 
    58 #<?php echo $i++ ?> <?php echo trim($sql) ?> 
     56<?php foreach(array_reverse(ActiveRecord::$query_log) as $query): ?> 
     57#<?php echo $i++ ?> <?php echo $query."\n" ?> 
    5958<?php endforeach; ?>         
    6059</code>