Show
Ignore:
Timestamp:
08/22/06 03:18:53 (6 years ago)
Author:
john
Message:

fixes to AR delete

Files:
1 modified

Legend:

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

    r227 r241  
    452452                $this->save_associations[$association_type][] = $value; 
    453453                if($association_type == "belongs_to") { 
    454                     $foreign_key = Inflector::singularize($value->table_name)."_id"; 
    455                     $this->$foreign_key = $value->id;  
     454                    $primary_key = $value->primary_keys[0]; 
     455                    $foreign_key = Inflector::singularize($value->table_name)."_".$primary_key; 
     456                    $this->$foreign_key = $value->$primary_key;  
    456457                } 
    457458            } 
     
    463464        }          
    464465         
    465     //  Assignment to something else, do it 
     466        //  Assignment to something else, do it 
    466467        $this->$key = $value; 
    467468    } 
     
    649650     */ 
    650651    private function find_all_has_many($other_table_name, $parameters = null) { 
    651  
     652        $additional_conditions = null; 
    652653        # Use any passed-in parameters 
    653654        if (is_array($parameters)) { 
     
    704705             
    705706            $foreign_key_value = $this->$this_primary_key; 
    706             $conditions = is_numeric($foreign_key_value) ?  
     707            $conditions = (is_numeric($foreign_key_value) ?  
    707708                "$foreign_key = {$foreign_key_value}" :  
    708                 "$foreign_key = '{$foreign_key_value}'" . $additional_conditions;  
     709                "$foreign_key = '{$foreign_key_value}'") . $additional_conditions;  
    709710        } 
    710711                          
     
    722723     */ 
    723724    private function find_one_has_one($other_object_name, $parameters = null) { 
    724  
     725         
     726        $additional_conditions = null; 
    725727        # Use any passed-in parameters 
    726728        if (is_array($parameters)) { 
     
    757759 
    758760        $foreign_key_value = $this->$this_primary_key; 
    759         $conditions = is_numeric($foreign_key_value) ?  
     761        $conditions = (is_numeric($foreign_key_value) ?  
    760762            "{$foreign_key} = {$foreign_key_value}" :  
    761             "{$foreign_key} = '{$foreign_key_value}'";  
     763            "{$foreign_key} = '{$foreign_key_value}'") . $additional_conditions;  
    762764         
    763765        # Get the list of other_class_name objects 
     
    782784    private function find_one_belongs_to($other_object_name, $parameters = null) { 
    783785 
     786        $additional_conditions = null; 
    784787        # Use any passed-in parameters 
    785788        if (is_array($parameters)) { 
    786789            //echo "<pre>";print_r($parameters); 
    787790            if(@array_key_exists("conditions", $parameters)) { 
    788                 $additional_conditions = $parameters['conditions']; 
     791                $additional_conditions = " AND (".$parameters['conditions'].")"; 
    789792            } elseif($parameters[0] != "") { 
    790                 $additional_conditions = $parameters[0]; 
     793                $additional_conditions = " AND (".$parameters[0].")"; 
    791794            } 
    792795            if(@array_key_exists("order", $parameters)) { 
     
    816819         
    817820        $other_primary_key_value = $this->$foreign_key; 
    818         $conditions = is_numeric($other_primary_key_value) ?  
     821        $conditions = (is_numeric($other_primary_key_value) ?  
    819822            "{$other_primary_key} = {$other_primary_key_value}" :  
    820             "{$other_primary_key} = '{$other_primary_key_value}'"; 
     823            "{$other_primary_key} = '{$other_primary_key_value}'") . $additional_conditions; 
    821824         
    822825        # Get the list of other_class_name objects 
     
    11291132        $offset = null; 
    11301133        $per_page = null; 
     1134        $select = null; 
    11311135 
    11321136        # this is if they passed in an associative array to emulate 
     
    11501154            $sql = $conditions; 
    11511155        } else { 
     1156 
     1157            # If select fields not specified just do a SELECT * 
     1158            if(is_null($select)) { 
     1159                $select = "*"; 
     1160            } 
     1161 
    11521162            # SQL will be built from specifications in argument 
    1153             $sql  = "SELECT * FROM ".$this->table_name." ";          
     1163            $sql  = "SELECT {$select} FROM {$this->table_name} ";          
    11541164             
    11551165            # If join specified, include it 
    11561166            if(!is_null($joins)) { 
    1157                 if(substr($joins,0,4) != "LEFT") $sql .= ","; 
    11581167                $sql .= " $joins "; 
    11591168            } 
     
    12191228            $object = new $class_name(); 
    12201229            $object->new_record = false; 
     1230            $objects_key = null; 
    12211231            foreach($row as $field => $value) { 
    12221232                $object->$field = $value; 
     
    12271237            $objects[$objects_key] = $object; 
    12281238            unset($object); 
    1229             unset($objects_key); 
    12301239        } 
    12311240        return $objects; 
     
    13261335     *  @throws {@link ActiveRecordError} 
    13271336     */ 
    1328     function find_first($conditions, $order = null, $limit = null, $joins = null) { 
     1337    function find_first($conditions = null, $order = null, $limit = null, $joins = null) { 
    13291338        if(is_array($conditions)) { 
    13301339            $options = $conditions;     
     
    15421551        } else { 
    15431552            $habtm_result = true; 
    1544             $this->id = $this->get_insert_id(); 
    1545             if($this->id > 0) { 
     1553            $primary_key = $this->primary_keys[0]; 
     1554            $primary_key_value = $this->get_insert_id(); 
     1555             $this->$primary_key = $primary_key_value; 
     1556            if($primary_key_value > 0) { 
    15461557                if($this->auto_save_habtm) { 
    1547                     $habtm_result = $this->add_habtm_records($this->id); 
     1558                    $habtm_result = $this->add_habtm_records($primary_key_value); 
    15481559                } 
    15491560                $this->save_associations(); 
     
    15781589        $updates = $this->get_updates_sql(); 
    15791590        $conditions = $this->get_primary_key_conditions(); 
    1580         $sql = "UPDATE $this->table_name SET $updates WHERE $conditions"; 
     1591        $sql = "UPDATE {$this->table_name} SET {$updates} WHERE {$conditions}"; 
     1592        //echo "update_record:$sql<br>"; 
    15811593        //error_log("update_record: SQL: $sql<br>"); 
    15821594        $result = $this->query($sql); 
     
    15851597        } else { 
    15861598            $habtm_result = true; 
    1587             if($this->id > 0) { 
     1599            $primary_key = $this->primary_keys[0]; 
     1600            $primary_key_value = $this->$primary_key; 
     1601            if($primary_key_value > 0) {  
    15881602                if($this->auto_save_habtm) { 
    1589                     $habtm_result = $this->update_habtm_records($this->id); 
     1603                    $habtm_result = $this->update_habtm_records($primary_key_value); 
    15901604                } 
    15911605                $this->save_associations(); 
     
    16251639        } 
    16261640        if(is_string($this->has_one)) { 
    1627             if(preg_match("/\b$association_name/\b", $this->has_one)) { 
     1641            if(preg_match("/\b$association_name\b/", $this->has_one)) { 
    16281642                $type = "has_one";      
    16291643            } 
     
    16871701                case "has_many": 
    16881702                case "has_one": 
    1689                     $foreign_key = Inflector::singularize($this->table_name)."_id"; 
    1690                     $object->$foreign_key = $this->id;  
    1691                     //echo "fk:$foreign_key = $this->id<br>"; 
     1703                    $primary_key = $this->primary_keys[0]; 
     1704                    $foreign_key = Inflector::singularize($this->table_name)."_".$primary_key; 
     1705                    $object->$foreign_key = $this->$primary_key;  
     1706                    //echo "fk:$foreign_key = ".$this->$primary_key."<br>"; 
    16921707                    break; 
    16931708            } 
     
    17051720     */ 
    17061721    function delete($id = null) { 
    1707         if($this->id > 0 && is_null($id)) { 
    1708             $id = $this->id; 
    1709         } 
    1710  
     1722 
     1723        $primary_key_value = null; 
     1724        $primary_key = $this->primary_keys[0]; 
    17111725        if(is_null($id)) { 
    1712             $this->errors[] = "No id specified to delete on."; 
     1726            # Primary key's where clause from already loaded values 
     1727            $conditions = $this->get_primary_key_conditions(); 
     1728            $primary_key_value = $this->$primary_key; 
     1729        } elseif(!is_array($id)) {  
     1730            $primary_key_value = $id;          
     1731            $id = is_numeric($id) ? $id : "'".$id."'"; 
     1732            $conditions = "{$primary_key} = $id"; 
     1733        } elseif(is_array($id)) { 
     1734            //$ids = ($this->attribute_is_string($primary_key)) ? "'".implode("','", $id)."'" : implode(',', $id); 
     1735            //$conditions = "{$primary_key} IN ($ids)"; 
     1736        } 
     1737 
     1738        if(is_null($conditions)) { 
     1739            $this->errors[] = "No conditions specified to delete on."; 
    17131740            return false; 
    17141741        } 
    17151742 
    1716         $this->before_delete(); 
    1717         $result = $this->delete_all("id IN ($id)"); 
    1718         if($this->auto_delete_habtm) { 
     1743        $this->before_delete();  
     1744        $result = $this->delete_all($conditions); 
     1745        if($this->auto_delete_habtm && $primary_key_value != '') { 
    17191746            if(is_string($this->has_and_belongs_to_many)) { 
    17201747                $habtms = explode(",", $this->has_and_belongs_to_many); 
    17211748                foreach($habtms as $other_table_name) { 
    1722                     $this->delete_all_habtm_records(trim($other_table_name), $id);                              
     1749                    $this->delete_all_habtm_records(trim($other_table_name), $primary_key_value);                              
    17231750                } 
    17241751            } elseif(is_array($this->has_and_belongs_to_many)) { 
    17251752                foreach($this->has_and_belongs_to_many as $other_table_name => $values) { 
    1726                     $this->delete_all_habtm_records($other_table_name, $id);                              
     1753                    $this->delete_all_habtm_records($other_table_name, $primary_key_value);                              
    17271754                } 
    17281755            }  
     
    17781805                if(is_array($habtm_array)) { 
    17791806                    if(is_string($this->has_and_belongs_to_many)) { 
    1780                         if(preg_match("/$key/", $this->has_and_belongs_to_many)) { 
     1807                        if(preg_match("/\b$key\b/", $this->has_and_belongs_to_many)) { 
    17811808                            $this->habtm_attributes[$key] = $habtm_array; 
    17821809                        } 
     
    19691996                    $this->save_associations[$association_type][] = $value; 
    19701997                    if($association_type == "belongs_to") { 
    1971                         $foreign_key = Inflector::singularize($value->table_name)."_id"; 
    1972                         $this->$foreign_key = $value->id;  
     1998                        $primary_key = $value->primary_keys[0]; 
     1999                        $foreign_key = Inflector::singularize($value->table_name)."_".$primary_key; 
     2000                        $this->$foreign_key = $value->$primary_key;  
    19732001                    } 
    19742002                } 
     
    21302158            # run through our fields and join them with their values 
    21312159            foreach($attributes as $key => $value) { 
    2132                 if($key && $value && !in_array($key, $this->primary_keys)) { 
     2160                if($key && ($value || is_numeric($value)) && !in_array($key, $this->primary_keys)) { 
    21332161                    $updates[] = "$key = $value"; 
    21342162                }