| 101 | | public $content_columns = null; # info about each column in the table |
| | 101 | public $content_columns = array(); # info about each column in the table |
| | 102 | |
| | 103 | /** |
| | 104 | * Table columns key => values array |
| | 105 | * |
| | 106 | * Array to hold all the column names and values. |
| | 107 | * @var array |
| | 108 | * @see __get() |
| | 109 | * @see __set() |
| | 110 | */ |
| | 111 | private $attributes = array(); |
| | 112 | |
| | 113 | /** |
| | 114 | * Array to hold all the changed columns and what changed. Indexed on column name. |
| | 115 | * |
| | 116 | * <p>calling $user->changes => Array( 'first_name' => array('original' => 'John', 'modified' => 'Matt') )</p> |
| | 117 | * <p>calling $user->changed => Array( 'first_name' )</p> |
| | 118 | * <p>calling $user->first_name_change => array('original' => 'John', 'modified' => 'Matt')</p> |
| | 119 | * <p>calling $user->first_name_changed => true</p> |
| | 120 | * |
| | 121 | * @var array |
| | 122 | * @see __get() |
| | 123 | * @see __set() |
| | 124 | */ |
| | 125 | private $changed_attributes = array(); |
| | 126 | |
| | 127 | /** |
| | 128 | * Whether or not to use partial updates meaning only |
| | 129 | * update fields that have changed in UPDATE sql calls |
| | 130 | * |
| | 131 | * @var boolean |
| | 132 | */ |
| | 133 | public $partial_updates = true; |
| 603 | | } |
| | 642 | } elseif($key == "changes") { |
| | 643 | return $this->changed_attributes; |
| | 644 | } elseif($key == "changed") { |
| | 645 | return array_keys($this->changed_attributes); |
| | 646 | } elseif(substr($key, -7) == "_change") { |
| | 647 | $changes = array(); |
| | 648 | $attribute = substr($key, 0, -7); |
| | 649 | if(array_key_exists($attribute, $this->changed_attributes)) { |
| | 650 | $changes = $this->changed_attributes[$attribute]; |
| | 651 | } |
| | 652 | return $changes; |
| | 653 | } elseif(substr($key, -8) == "_changed") { |
| | 654 | $attribute = substr($key, 0, -8); |
| | 655 | return array_key_exists($attribute, $this->changed_attributes) ? true : false; |
| | 656 | } |
| 623 | | if($key == "table_name") { |
| | 676 | if(array_key_exists($key, (array)$this->content_columns)) { |
| | 677 | if(array_key_exists($key, $this->attributes)) { |
| | 678 | if(array_key_exists($key, $this->changed_attributes)) { |
| | 679 | if($this->changed_attributes[$key]['original'] != $value) { |
| | 680 | $this->changed_attributes[$key]['modified'] = $value; |
| | 681 | } else { |
| | 682 | unset($this->changed_attributes[$key]); |
| | 683 | } |
| | 684 | } else { |
| | 685 | $this->changed_attributes[$key] = array( |
| | 686 | 'original' => $this->attributes[$key], |
| | 687 | 'modified' => $value |
| | 688 | ); |
| | 689 | } |
| | 690 | } |
| | 691 | $this->attributes[$key] = $value; |
| | 692 | return; |
| | 693 | } elseif($key == "table_name") { |
| 1198 | | if(is_array($this->content_columns)) { |
| 1199 | | foreach($this->content_columns as $column) { |
| 1200 | | if($column['name'] == $attribute) { |
| 1201 | | return true; |
| 1202 | | } |
| 1203 | | } |
| 1204 | | } |
| 1205 | | return false; |
| 1206 | | } |
| | 1262 | return array_key_exists($attribute, (array)$this->content_columns) ? true : false; |
| | 1263 | } |
| | 1264 | |
| | 1265 | /** |
| | 1266 | * Resets the changed_attributes array back to empty |
| | 1267 | * |
| | 1268 | * @uses changed_attributes |
| | 1269 | */ |
| | 1270 | private function clear_changed_attributes() { |
| | 1271 | $this->changed_attributes = array(); |
| | 1272 | } |
| 2022 | | //error_log("update_record: SQL: $sql"); |
| 2023 | | $result = $this->query($sql); |
| 2024 | | $habtm_result = true; |
| 2025 | | $primary_key = $this->primary_keys[0]; |
| 2026 | | $primary_key_value = $this->$primary_key; |
| 2027 | | if($primary_key_value > 0) { |
| 2028 | | if($this->auto_save_habtm) { |
| 2029 | | $habtm_result = $this->update_habtm_records($primary_key_value); |
| 2030 | | } |
| 2031 | | $this->save_associations(); |
| 2032 | | } |
| | 2097 | #error_log("update_record: SQL: $sql"); |
| | 2098 | if($updates && $conditions) { |
| | 2099 | $result = $this->query($sql); |
| | 2100 | $habtm_result = true; |
| | 2101 | $primary_key = $this->primary_keys[0]; |
| | 2102 | $primary_key_value = $this->$primary_key; |
| | 2103 | if($primary_key_value > 0) { |
| | 2104 | if($this->auto_save_habtm) { |
| | 2105 | $habtm_result = $this->update_habtm_records($primary_key_value); |
| | 2106 | } |
| | 2107 | $this->save_associations(); |
| | 2108 | } |
| | 2109 | } |
| 2161 | | if(is_object($object) && get_parent_class($object) == __CLASS__ && $type) { |
| 2162 | | //echo get_class($object)." - type:$type<br>"; |
| 2163 | | switch($type) { |
| 2164 | | case "has_many": |
| 2165 | | case "has_one": |
| 2166 | | $primary_key = $this->primary_keys[0]; |
| 2167 | | $foreign_key = Inflector::singularize($this->table_name)."_".$primary_key; |
| 2168 | | $object->$foreign_key = $this->$primary_key; |
| 2169 | | //echo "fk:$foreign_key = ".$this->$primary_key."<br>"; |
| 2170 | | break; |
| 2171 | | } |
| 2172 | | $object->save(); |
| | 2238 | if(is_object($object) && get_parent_class($object) == __CLASS__ && $type) { |
| | 2239 | if($object->changed) { |
| | 2240 | //echo get_class($object)." - type:$type<br>"; |
| | 2241 | switch($type) { |
| | 2242 | case "has_many": |
| | 2243 | case "has_one": |
| | 2244 | $primary_key = $this->primary_keys[0]; |
| | 2245 | $foreign_key = Inflector::singularize($this->table_name)."_".$primary_key; |
| | 2246 | $object->$foreign_key = $this->$primary_key; |
| | 2247 | //echo "fk:$foreign_key = ".$this->$primary_key."<br>"; |
| | 2248 | break; |
| | 2249 | } |
| | 2250 | $object->save(); |
| | 2251 | } |
| 2387 | | if($this->auto_timestamps) { |
| 2388 | | if(is_array($this->content_columns)) { |
| 2389 | | foreach($this->content_columns as $field_info) { |
| 2390 | | if(($field_info['name'] == $field) && stristr($field_info['type'], "date")) { |
| 2391 | | $format = ($field_info['type'] == "date") ? $this->date_format : "{$this->date_format} {$this->time_format}"; |
| 2392 | | if($this->new_record) { |
| 2393 | | if(in_array($field, $this->auto_create_timestamps)) { |
| 2394 | | return date($format); |
| 2395 | | } elseif($this->preserve_null_dates && is_null($value) && !stristr($field_info['flags'], "not_null")) { |
| 2396 | | return null; |
| 2397 | | } |
| 2398 | | } elseif(!$this->new_record) { |
| 2399 | | if(in_array($field, $this->auto_update_timestamps)) { |
| 2400 | | return date($format); |
| 2401 | | } elseif($this->preserve_null_dates && is_null($value) && !stristr($field_info['flags'], "not_null")) { |
| 2402 | | return null; |
| 2403 | | } |
| | 2466 | if($this->auto_timestamps) { |
| | 2467 | if(array_key_exists($field, (array)$this->content_columns)) { |
| | 2468 | if(stristr($this->content_columns[$field]['type'], "date")) { |
| | 2469 | $format = ($this->content_columns[$field]['type'] == "date") ? $this->date_format : "{$this->date_format} {$this->time_format}"; |
| | 2470 | if($this->new_record) { |
| | 2471 | if(in_array($field, $this->auto_create_timestamps) || in_array($field, $this->auto_update_timestamps)) { |
| | 2472 | $date = date($format); |
| | 2473 | $this->$field = $date; |
| | 2474 | return $date; |
| | 2475 | } elseif($this->preserve_null_dates && is_null($value) && !stristr($this->content_columns[$field]['flags'], "not_null")) { |
| | 2476 | return null; |
| 2604 | | * Return column values for SQL insert statement |
| 2605 | | * |
| 2606 | | * Return an array containing the column names and values of this |
| 2607 | | * object, filtering out the primary keys, which are not set. |
| 2608 | | * |
| 2609 | | * @uses $primary_keys |
| 2610 | | * @uses quoted_attributes() |
| 2611 | | */ |
| 2612 | | function get_inserts() { |
| 2613 | | $attributes = $this->quoted_attributes(); |
| 2614 | | $inserts = array(); |
| 2615 | | foreach($attributes as $key => $value) { |
| 2616 | | if(!in_array($key, $this->primary_keys) || ($value != "''" && isset($value))) { |
| 2617 | | $inserts[$key] = $value; |
| 2618 | | } |
| 2619 | | } |
| 2620 | | return $inserts; |
| 2621 | | } |
| 2622 | | |
| 2623 | | /** |
| | 2715 | * Return column values for SQL insert statement |
| | 2716 | * |
| | 2717 | * Return an array containing the column names and values of this |
| | 2718 | * object, filtering out the primary keys, which are not set. |
| | 2719 | * |
| | 2720 | * @uses $primary_keys |
| | 2721 | * @uses quoted_attributes() |
| | 2722 | */ |
| | 2723 | function get_inserts() { |
| | 2724 | $attributes = $this->quoted_attributes(); |
| | 2725 | $inserts = array(); |
| | 2726 | foreach($attributes as $key => $value) { |
| | 2727 | if(!in_array($key, $this->primary_keys) || ($value != "''" && isset($value))) { |
| | 2728 | $inserts[$key] = $value; |
| | 2729 | } |
| | 2730 | } |
| | 2731 | return $inserts; |
| | 2732 | } |
| | 2733 | |
| | 2734 | /** |
| 2667 | | $attributes = $this->quoted_attributes(); |
| | 2746 | $attributes = null; |
| | 2747 | if($this->partial_updates && $this->changed) { |
| | 2748 | foreach($this->changed_attributes as $key => $changes) { |
| | 2749 | $attributes[$key] = $changes['modified']; |
| | 2750 | } |
| | 2751 | #error_log("modified attributes:".print_r($attributes, true)); |
| | 2752 | # make sure the auto timestamps are in there |
| | 2753 | foreach($this->content_columns as $column_name => $column) { |
| | 2754 | if(stristr($column['type'], "date") && in_array($column_name, $this->auto_update_timestamps) && !array_key_exists($column_name, $attributes)) { |
| | 2755 | $attributes[$column_name] = $this->$column_name; |
| | 2756 | } |
| | 2757 | } |
| | 2758 | #error_log("modified attributes with dates:".print_r($attributes, true)); |
| | 2759 | } |
| | 2760 | $attributes = $this->quoted_attributes($attributes); |
| 2732 | | $this->content_columns = $db->reverse->tableInfo($table_name); |
| 2733 | | if($this->is_error($this->content_columns)) { |
| 2734 | | $this->raise($this->content_columns->getMessage()); |
| 2735 | | } |
| 2736 | | if(is_array($this->content_columns)) { |
| 2737 | | $i = 0; |
| 2738 | | foreach($this->content_columns as $column) { |
| 2739 | | $this->content_columns[$i++]['human_name'] = $this->human_attribute_name($column['name']); |
| 2740 | | } |
| | 2826 | $content_columns = $db->reverse->tableInfo($table_name); |
| | 2827 | if($this->is_error($content_columns)) { |
| | 2828 | $this->raise($content_columns->getMessage()); |
| | 2829 | } |
| | 2830 | if(is_array($content_columns)) { |
| | 2831 | $this->content_columns = array(); |
| | 2832 | foreach($content_columns as $column) { |
| | 2833 | $column['human_name'] = $this->human_attribute_name($column['name']); |
| | 2834 | $this->content_columns[$column['name']] = $column; |
| | 2835 | } |