| | 166 | } |
|---|
| | 167 | elseif (in_array($key, $this->has_and_belongs_to_many)) |
|---|
| | 168 | { |
|---|
| | 169 | if (empty($this->object_related[$key]['cur'])) |
|---|
| | 170 | { |
|---|
| | 171 | // Foreign key |
|---|
| | 172 | $fk = inflector::singular($key).'_id'; |
|---|
| | 173 | |
|---|
| | 174 | // Query to find relationships |
|---|
| | 175 | $query = self::$db |
|---|
| | 176 | ->select($fk) |
|---|
| | 177 | ->from($this->related_table($key)) |
|---|
| | 178 | ->where($this->class.'_id', $this->id) |
|---|
| | 179 | ->get(); |
|---|
| | 180 | |
|---|
| | 181 | foreach ($query as $row) |
|---|
| | 182 | { |
|---|
| | 183 | // Set relationships |
|---|
| | 184 | $this->object_related[$key]['cur'][] = $row->$fk; |
|---|
| | 185 | } |
|---|
| | 186 | } |
|---|
| | 187 | |
|---|
| | 188 | // Return the current relationships |
|---|
| | 189 | return $this->object_related[$key]['cur']; |
|---|
| 756 | | $data = array(); |
|---|
| 757 | | foreach ($this->changed as $key) |
|---|
| 758 | | { |
|---|
| 759 | | // Get changed data |
|---|
| 760 | | $data[$key] = $this->object->$key; |
|---|
| 761 | | } |
|---|
| 762 | | |
|---|
| 763 | | if (empty($this->object->id)) |
|---|
| 764 | | { |
|---|
| 765 | | // Perform an insert |
|---|
| 766 | | $query = self::$db->insert($this->table, $data); |
|---|
| | 796 | if ( ! empty($this->changed)) |
|---|
| | 797 | { |
|---|
| | 798 | $data = array(); |
|---|
| | 799 | foreach ($this->changed as $key) |
|---|
| | 800 | { |
|---|
| | 801 | // Get changed data |
|---|
| | 802 | $data[$key] = $this->object->$key; |
|---|
| | 803 | } |
|---|
| | 804 | |
|---|
| | 805 | if (empty($this->object->id)) |
|---|
| | 806 | { |
|---|
| | 807 | // Perform an insert |
|---|
| | 808 | $query = self::$db->insert($this->table, $data); |
|---|
| | 809 | |
|---|
| | 810 | if ($query->count()) |
|---|
| | 811 | { |
|---|
| | 812 | // Set current object id by the insert id |
|---|
| | 813 | $this->object->id = $query->insert_id(); |
|---|
| | 814 | } |
|---|
| | 815 | } |
|---|
| | 816 | else |
|---|
| | 817 | { |
|---|
| | 818 | // Perform an update |
|---|
| | 819 | $query = self::$db->update($this->table, $data, array('id' => $this->object->id)); |
|---|
| | 820 | } |
|---|
| 770 | | // Set current object id by the insert id |
|---|
| 771 | | $this->object->id = $query->insert_id(); |
|---|
| 772 | | } |
|---|
| 773 | | } |
|---|
| 774 | | else |
|---|
| 775 | | { |
|---|
| 776 | | // Perform an update |
|---|
| 777 | | $query = self::$db->update($this->table, $data, array('id' => $this->object->id)); |
|---|
| 778 | | } |
|---|
| 779 | | |
|---|
| 780 | | if ($query->count()) |
|---|
| 781 | | { |
|---|
| 782 | | // Reset changed data |
|---|
| 783 | | $this->changed = array(); |
|---|
| 784 | | |
|---|
| 785 | | // Reset SELECT, WHERE, and FROM |
|---|
| 786 | | $this->select = $this->where = $this->from = FALSE; |
|---|
| | 824 | // Reset changed data |
|---|
| | 825 | $this->changed = array(); |
|---|
| | 826 | |
|---|
| | 827 | // Reset SELECT, WHERE, and FROM |
|---|
| | 828 | $this->select = $this->where = $this->from = FALSE; |
|---|
| | 829 | |
|---|
| | 830 | // Object has been loaded and saved |
|---|
| | 831 | $this->loaded = $this->saved = TRUE; |
|---|
| | 832 | } |
|---|
| | 833 | } |
|---|
| | 834 | |
|---|
| | 835 | if ( ! empty($this->changed_related) AND ($this->saved OR $this->loaded)) |
|---|
| | 836 | { |
|---|
| | 837 | foreach ($this->changed_related as $table) |
|---|
| | 838 | { |
|---|
| | 839 | // Skip empty rows |
|---|
| | 840 | if (empty($this->object_related[$table])) |
|---|
| | 841 | continue; |
|---|
| | 842 | |
|---|
| | 843 | // Current and new relationships |
|---|
| | 844 | $cur = $this->object_related[$table]['cur']; |
|---|
| | 845 | $new = $this->object_related[$table]['new']; |
|---|
| | 846 | |
|---|
| | 847 | // Foreign and primary keys |
|---|
| | 848 | $fk = inflector::singular($table).'_id'; |
|---|
| | 849 | $pk = $this->class.'_id'; |
|---|
| | 850 | |
|---|
| | 851 | // Relationship table |
|---|
| | 852 | $table = $this->related_table($table); |
|---|
| | 853 | |
|---|
| | 854 | if ($delete = array_diff($cur, $new)) |
|---|
| | 855 | { |
|---|
| | 856 | // Delete relationships |
|---|
| | 857 | self::$db->in($fk, $delete)->where($pk, $this->id)->delete($table); |
|---|
| | 858 | } |
|---|
| | 859 | |
|---|
| | 860 | foreach (array_diff($new, $cur) as $key) |
|---|
| | 861 | { |
|---|
| | 862 | // Create new relationships |
|---|
| | 863 | self::$db->insert($table, array($pk => $this->id, $fk => $key)); |
|---|
| | 864 | } |
|---|
| | 865 | } |
|---|