Changeset 261

Show
Ignore:
Timestamp:
09/03/06 20:03:16 (6 years ago)
Author:
john
Message:

added support for composed_of - composite value objects in AR

Files:
1 modified

Legend:

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

    r259 r261  
    104104     *  Table Info 
    105105     * 
    106      *  Array to hold all the info about table columns.  Index on $table_name. 
     106     *  Array to hold all the info about table columns.  Indexed on $table_name. 
    107107     *  @var array 
    108108     */     
     
    162162     
    163163    /** 
    164      * Stores the active connections 
     164     * Stores the active connections. Indexed on $connection_name. 
    165165     */ 
    166166    public static $active_connections = array(); 
     
    197197     */     
    198198    public $lock_optimistically = true; 
     199     
     200    /** 
     201     *  Composite custom user created objects 
     202     *  @var mixed 
     203     */     
     204    public $composed_of = null; 
    199205 
    200206    # Table associations 
     
    447453     */ 
    448454    function __get($key) { 
    449         $association_type = $this->get_association_type($key); 
    450         if (is_null($association_type)) { 
    451             return null; 
    452         } 
    453         //error_log("association_type:$association_type"); 
    454         switch($association_type) { 
    455             case "has_many": 
    456                 $parameters = is_array($this->has_many) ? $this->has_many[$key] : null; 
    457                 $this->$key = $this->find_all_has_many($key, $parameters); 
    458                 break; 
    459             case "has_one": 
    460                 $parameters = is_array($this->has_one) ? $this->has_one[$key] : null; 
    461                 $this->$key = $this->find_one_has_one($key, $parameters); 
    462                 break; 
    463             case "belongs_to": 
    464                 $parameters = is_array($this->belongs_to) ? $this->belongs_to[$key] : null; 
    465                 $this->$key = $this->find_one_belongs_to($key, $parameters); 
    466                 break; 
    467             case "has_and_belongs_to_many":   
    468                 $parameters = is_array($this->has_and_belongs_to_many) ? $this->has_and_belongs_to_many[$key] : null; 
    469                 $this->$key = $this->find_all_habtm($key, $parameters);  
    470                 break;             
    471         } 
    472         //echo "<pre>id: $this->id<br>getting: $key = ".$this->$key."<br></pre>"; 
     455        if($association_type = $this->get_association_type($key)) { 
     456            //error_log("association_type:$association_type"); 
     457            switch($association_type) { 
     458                case "has_many": 
     459                    $parameters = is_array($this->has_many) ? $this->has_many[$key] : null; 
     460                    $this->$key = $this->find_all_has_many($key, $parameters); 
     461                    break; 
     462                case "has_one": 
     463                    $parameters = is_array($this->has_one) ? $this->has_one[$key] : null; 
     464                    $this->$key = $this->find_one_has_one($key, $parameters); 
     465                    break; 
     466                case "belongs_to": 
     467                    $parameters = is_array($this->belongs_to) ? $this->belongs_to[$key] : null; 
     468                    $this->$key = $this->find_one_belongs_to($key, $parameters); 
     469                    break; 
     470                case "has_and_belongs_to_many":   
     471                    $parameters = is_array($this->has_and_belongs_to_many) ? $this->has_and_belongs_to_many[$key] : null; 
     472                    $this->$key = $this->find_all_habtm($key, $parameters);  
     473                    break;             
     474            }         
     475        } elseif($this->is_composite($key)) { 
     476            $composite_object = $this->get_composite_object($key); 
     477            if(is_object($composite_object)) { 
     478                $this->$key = $composite_object;     
     479            }                                 
     480        }  
     481        //echo "<pre>getting: $key = ".$this->$key."<br></pre>"; 
    473482        return $this->$key; 
    474483    } 
     
    506515                $this->save_associations[$association_type][] = $value; 
    507516            } 
    508         }          
     517        }        
    509518         
    510519        //  Assignment to something else, do it 
     
    575584        return $result; 
    576585    } 
    577  
     586     
    578587    /** 
    579588     *  Find all records using a "has_and_belongs_to_many" relationship 
     
    16201629            $this->raise($primary_key_value->getMessage()); 
    16211630        } 
    1622  
    16231631        $attributes = $this->get_inserts(); 
    16241632        $fields = @implode(', ', array_keys($attributes)); 
     
    16931701            return ($result && $habtm_result); 
    16941702        } 
     1703    } 
     1704 
     1705    /** 
     1706     *  Loads the model values into composite object 
     1707     *  @todo Document this API 
     1708     */     
     1709    private function get_composite_object($name) { 
     1710        $composite_object = null; 
     1711        if(is_array($this->composed_of)) { 
     1712            if(array_key_exists($name, $this->composed_of)) { 
     1713                $class_name = Inflector::classify($this->composed_of[$name]['class_name']); 
     1714                if(class_exists($class_name, false)) { 
     1715                    $composite_object = new $class_name; 
     1716                    $mappings = $this->composed_of[$name]['mapping']; 
     1717                    if(is_array($mappings)) { 
     1718                        foreach($mappings as $database_name => $composite_name) { 
     1719                            $composite_object->$composite_name = $this->$database_name;                           
     1720                        }     
     1721                    }       
     1722                } 
     1723            }     
     1724        } elseif($this->composed_of == $name) { 
     1725            $class_name = Inflector::classify($name); 
     1726            if(class_exists($class_name, false)) { 
     1727                $composite_object = new $class_name(); 
     1728                $composite_object->$name = $this->$name;             
     1729            } 
     1730        }  
     1731         
     1732        if(is_object($composite_object)) { 
     1733            if(method_exists($composite_object, 'initialize')) { 
     1734                $composite_object->initialize(); 
     1735            }             
     1736        } 
     1737         
     1738        return $composite_object; 
    16951739    } 
    16961740     
     
    21112155            } 
    21122156            $this->set_habtm_attributes($attributes); 
    2113         } 
    2114     } 
     2157            $this->update_composite_attributes(); 
     2158        } 
     2159    } 
     2160     
     2161    /** 
     2162     * If a composite object was specified via $composed_of, then its values  
     2163     * mapped to the model will overwrite the models values. 
     2164     * 
     2165     */ 
     2166    function update_composite_attributes() { 
     2167        if(is_array($this->composed_of)) { 
     2168            foreach($this->composed_of as $name => $options) { 
     2169                $composite_object = $this->$name; 
     2170                if(is_array($options) && is_object($composite_object)) { 
     2171                    if(is_array($options['mapping'])) { 
     2172                        foreach($options['mapping'] as $database_name => $composite_name) { 
     2173                            $this->$database_name = $composite_object->$composite_name;                           
     2174                        }     
     2175                    }         
     2176                }        
     2177            }     
     2178        }     
     2179    }     
    21152180 
    21162181    /** 
     
    24952560        return false;         
    24962561    } 
     2562 
     2563    /** 
     2564     *  Determine if passed in name is a composite class or not 
     2565     *  @param string $name Name of the composed_of mapping 
     2566     *  @uses $composed_of 
     2567     */     
     2568    private function is_composite($name) { 
     2569        if(is_array($this->composed_of)) { 
     2570            if(array_key_exists($name, $this->composed_of)) { 
     2571                return true;      
     2572            } 
     2573        }         
     2574        return false; 
     2575    }     
    24972576 
    24982577    /**