| 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>"; |
| | 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; |
| 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 | } |