Show
Ignore:
Timestamp:
04/18/06 11:43:51 (6 years ago)
Author:
john
Message:

added to AR

Files:
1 modified

Legend:

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

    r195 r197  
    167167     */ 
    168168    protected $save_associations = array(); 
     169     
    169170    /** 
    170171     *  @todo Document this property 
     
    303304     */ 
    304305    public $auto_save_habtm = true; # auto insert / update $has_and_belongs_to_many tables 
     306 
     307    /** 
     308     *  @todo Document this API 
     309     */     
     310    public $auto_delete_habtm = true; # auto delete $has_and_belongs_to_many associations 
    305311 
    306312    /** 
     
    527533        $other_foreign_key = Inflector::singularize($other_table_name)."_id"; 
    528534        # Set up the SQL segments 
    529         $conditions = "{$join_table}.{$this_foreign_key}={$this->id}"; 
     535        $conditions = "{$join_table}.{$this_foreign_key}=".intval($this->id); 
    530536        $orderings = null; 
    531537        $limit = null; 
     
    14251431                    $foreign_key = Inflector::singularize($this->table_name)."_id"; 
    14261432                    $object->$foreign_key = $this->id;  
    1427                     //echo "fk:$foreign_key = $this->id<br>"; 
     1433                    echo "fk:$foreign_key = $this->id<br>"; 
    14281434                    break; 
    14291435            } 
     
    14521458        $this->before_delete(); 
    14531459        $result = $this->delete_all("id IN ($id)"); 
     1460        if($this->auto_delete_habtm) { 
     1461            if(is_string($this->has_and_belongs_to_many)) { 
     1462                $habtms = explode(",", $this->has_and_belongs_to_many); 
     1463                foreach($habtms as $other_table_name) { 
     1464                    $this->delete_all_habtm_records(trim($other_table_name), $id);                              
     1465                } 
     1466            } elseif(is_array($this->has_and_belongs_to_many)) { 
     1467                foreach($this->has_and_belongs_to_many as $other_table_name => $values) { 
     1468                    $this->delete_all_habtm_records($other_table_name, $id);                              
     1469                } 
     1470            }  
     1471        } 
    14541472        $this->after_delete(); 
    14551473 
     
    15701588            reset($this->habtm_attributes); 
    15711589            foreach($this->habtm_attributes as $other_table_name => $values) { 
    1572                 $table_name = $this->get_join_table_name($this->table_name,$other_table_name); 
    1573                 $this_foreign_key = Inflector::singularize($this->table_name)."_id"; 
    1574                 $sql = "DELETE FROM $table_name WHERE $this_foreign_key = $this_foreign_value"; 
    1575                 //echo "delete_habtm_records: SQL: $sql<br>"; 
    1576                 $result = $this->query($sql); 
    1577                 if($this->is_error($result)) { 
    1578                     $this->raise($result->getMessage()); 
    1579                 } 
     1590                $this->delete_all_habtm_records($other_table_name, $this_foreign_value); 
    15801591            } 
    15811592        } 
    15821593        return true; 
     1594    } 
     1595     
     1596    private function delete_all_habtm_records($other_table_name, $this_foreign_value) { 
     1597        if($other_table_name && $this_foreign_value > 0) { 
     1598            $habtm_table_name = $this->get_join_table_name($this->table_name,$other_table_name); 
     1599            $this_foreign_key = Inflector::singularize($this->table_name)."_id"; 
     1600            $sql = "DELETE FROM $habtm_table_name WHERE $this_foreign_key = $this_foreign_value"; 
     1601            //echo "delete_all_habtm_records: SQL: $sql<br>"; 
     1602            $result = $this->query($sql); 
     1603            if($this->is_error($result)) { 
     1604                $this->raise($result->getMessage()); 
     1605            }             
     1606        } 
    15831607    } 
    15841608