1 | <?php defined('SYSPATH') OR die('No direct script access.'); |
||
11 | abstract class Kohana_Jam_Association_Collection extends Jam_Association { |
||
12 | |||
13 | /** |
||
14 | * Find the join table based on the two model names pluralized, |
||
15 | * sorted alphabetically and with an underscore separating them |
||
16 | * |
||
17 | * @param string $model1 |
||
18 | * @param string $model2 |
||
19 | * @return string |
||
20 | */ |
||
21 | 36 | static public function guess_through_table($model1, $model2) |
|
22 | { |
||
23 | $through = array( |
||
24 | 36 | Inflector::plural($model1), |
|
25 | 36 | Inflector::plural($model2) |
|
26 | ); |
||
27 | |||
28 | 36 | sort($through); |
|
29 | 36 | return implode('_', $through); |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * Assign default forein_model to singular association name |
||
34 | * @param Jam_Meta $meta |
||
35 | * @param string $name |
||
36 | */ |
||
37 | 93 | public function initialize(Jam_Meta $meta, $name) |
|
38 | { |
||
39 | 93 | if ( ! $this->foreign_model) |
|
40 | { |
||
41 | 79 | $this->foreign_model = Inflector::singular($name); |
|
42 | } |
||
43 | |||
44 | 93 | parent::initialize($meta, $name); |
|
45 | 93 | } |
|
46 | |||
47 | /** |
||
48 | * Load associated models (from database or after deserialization) |
||
49 | * @param Jam_Validated $model |
||
50 | * @param mixed $value |
||
51 | * @return Jam_Array_Association |
||
52 | */ |
||
53 | 7 | public function load_fields(Jam_Validated $model, $value) |
|
54 | { |
||
55 | 7 | if ($value instanceof Jam_Array_Association) |
|
56 | { |
||
57 | $collection = $value; |
||
58 | } |
||
59 | else |
||
60 | { |
||
61 | 7 | $collection = new Jam_Array_Association(); |
|
62 | $collection |
||
63 | 7 | ->load_fields($value); |
|
64 | } |
||
65 | |||
66 | 7 | return $this->assign_internals($model, $collection); |
|
67 | } |
||
68 | |||
69 | 754 | public function assign_internals(Jam_Validated $model, Jam_Array_Association $collection) |
|
70 | { |
||
71 | return $collection |
||
72 | 754 | ->model($this->foreign_model) |
|
73 | 754 | ->parent($model) |
|
74 | 754 | ->association($this); |
|
75 | } |
||
76 | |||
77 | 21 | public function get(Jam_Validated $model, $value, $is_changed) |
|
78 | { |
||
79 | 21 | $array = new Jam_Array_Association(); |
|
80 | $array |
||
81 | 21 | ->model($this->foreign_model) |
|
82 | 21 | ->association($this) |
|
83 | 21 | ->parent($model); |
|
84 | |||
85 | 21 | if ($is_changed) |
|
86 | { |
||
87 | 7 | $array->set($value); |
|
88 | } |
||
89 | |||
90 | 21 | return $array; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Perform checks on all changed items from this collection |
||
95 | * @param Jam_Model $model |
||
96 | */ |
||
97 | 8 | public function model_after_check(Jam_Model $model) |
|
98 | { |
||
99 | 8 | if ($model->changed($this->name) AND ! $model->{$this->name}->check_changed()) |
|
100 | { |
||
101 | $model->errors()->add($this->name, 'association_collection'); |
||
102 | } |
||
103 | 8 | } |
|
104 | |||
105 | /** |
||
106 | * Persist this collection in the database |
||
107 | * @param Jam_Model $model |
||
108 | */ |
||
109 | 8 | public function model_after_save(Jam_Model $model) |
|
110 | { |
||
111 | 8 | if ($model->changed($this->name) AND $collection = $model->{$this->name} AND $collection->changed()) |
|
112 | { |
||
113 | $collection->save_changed(); |
||
114 | |||
115 | $this->save($model, $collection); |
||
116 | } |
||
117 | 8 | } |
|
118 | |||
119 | /** |
||
120 | * Use this method to perform special actions when an item is requested from the collection |
||
121 | * @param Jam_Model $model |
||
122 | * @param Jam_Model $item |
||
123 | */ |
||
124 | public function item_get(Jam_Model $model, Jam_Model $item) |
||
125 | { |
||
126 | // Extend |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Use this method to perform special actions when an item is set to the collection |
||
131 | * @param Jam_Model $model |
||
132 | * @param Jam_Model $item |
||
133 | */ |
||
134 | public function item_set(Jam_Model $model, Jam_Model $item) |
||
135 | { |
||
136 | // Extend |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Use this method to perform special actions when an item is removed from the collection |
||
141 | * @param Jam_Model $model |
||
142 | * @param Jam_Model $item |
||
143 | */ |
||
144 | public function item_unset(Jam_Model $model, Jam_Model $item) |
||
|
|||
145 | { |
||
146 | // Extend |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * A database query used to remove items from the model |
||
151 | * @param array $ids |
||
152 | * @param Jam_Model $model |
||
153 | * @return Database_Query |
||
154 | */ |
||
155 | abstract public function remove_items_query(Jam_Model $model, array $ids); |
||
156 | |||
157 | /** |
||
158 | * A database query used to add items to the model |
||
159 | * @param array $ids |
||
160 | * @param Jam_Model $model |
||
161 | * @return Database_Query |
||
162 | */ |
||
163 | abstract public function add_items_query(Jam_Model $model, array $ids); |
||
164 | |||
165 | /** |
||
166 | * Get the colleciton to get the models of the association |
||
167 | * @param Jam_Model $model |
||
168 | * @return Jam_Query_Builder_Collection |
||
169 | */ |
||
170 | abstract public function collection(Jam_Model $model); |
||
171 | |||
172 | /** |
||
173 | * Execute remove_items_query and add_items_query to persist the colleciton to the datbaase |
||
174 | * @param Jam_Model $model |
||
175 | * @param Jam_Array_Association $collection |
||
176 | */ |
||
177 | 1 | public function save(Jam_Model $model, Jam_Array_Association $collection) |
|
178 | { |
||
179 | 1 | if ($old_ids = array_values(array_diff($collection->original_ids(), $collection->ids()))) |
|
197 | |||
198 | /** |
||
199 | * Use the remove query to remove all items from the collection |
||
200 | * @param Jam_Validated $model |
||
201 | * @param Jam_Array_Association $collection |
||
202 | */ |
||
203 | 1 | public function clear(Jam_Validated $model, Jam_Array_Association $collection) |
|
214 | |||
215 | } |
||
216 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.