|
1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
|
2
|
|
|
/** |
|
3
|
|
|
* Common association for has-many and many-to-many relationships |
|
4
|
|
|
* |
|
5
|
|
|
* @package Jam |
|
6
|
|
|
* @category Associations |
|
7
|
|
|
* @author Ivan Kerin |
|
8
|
|
|
* @copyright (c) 2012 Despark Ltd. |
|
9
|
|
|
* @license http://www.opensource.org/licenses/isc-license.txt |
|
10
|
|
|
*/ |
|
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
|
30 |
|
static public function guess_through_table($model1, $model2) |
|
22
|
|
|
{ |
|
23
|
|
|
$through = array( |
|
24
|
30 |
|
Inflector::plural($model1), |
|
25
|
30 |
|
Inflector::plural($model2) |
|
26
|
30 |
|
); |
|
27
|
|
|
|
|
28
|
30 |
|
sort($through); |
|
29
|
30 |
|
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
|
80 |
|
public function initialize(Jam_Meta $meta, $name) |
|
38
|
|
|
{ |
|
39
|
80 |
|
if ( ! $this->foreign_model) |
|
40
|
80 |
|
{ |
|
41
|
67 |
|
$this->foreign_model = Inflector::singular($name); |
|
42
|
67 |
|
} |
|
43
|
|
|
|
|
44
|
80 |
|
parent::initialize($meta, $name); |
|
45
|
80 |
|
} |
|
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
|
7 |
|
{ |
|
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
|
643 |
|
public function assign_internals(Jam_Validated $model, Jam_Array_Association $collection) |
|
70
|
|
|
{ |
|
71
|
|
|
return $collection |
|
72
|
643 |
|
->model($this->foreign_model) |
|
73
|
643 |
|
->parent($model) |
|
74
|
643 |
|
->association($this); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
22 |
|
public function get(Jam_Validated $model, $value, $is_changed) |
|
78
|
|
|
{ |
|
79
|
22 |
|
$array = new Jam_Array_Association(); |
|
80
|
|
|
$array |
|
81
|
22 |
|
->model($this->foreign_model) |
|
82
|
22 |
|
->association($this) |
|
83
|
22 |
|
->parent($model); |
|
84
|
|
|
|
|
85
|
|
|
if ($is_changed) |
|
86
|
22 |
|
{ |
|
87
|
8 |
|
$array->set($value); |
|
88
|
8 |
|
} |
|
89
|
|
|
|
|
90
|
22 |
|
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
|
8 |
|
{ |
|
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
|
8 |
|
{ |
|
113
|
1 |
|
$collection->save_changed(); |
|
114
|
|
|
|
|
115
|
1 |
|
$this->save($model, $collection); |
|
116
|
1 |
|
} |
|
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
|
1 |
|
public function item_get(Jam_Model $model, Jam_Model $item) |
|
125
|
|
|
{ |
|
126
|
|
|
// Extend |
|
127
|
1 |
|
} |
|
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
|
2 |
|
public function save(Jam_Model $model, Jam_Array_Association $collection) |
|
178
|
|
|
{ |
|
179
|
2 |
|
if ($old_ids = array_values(array_diff($collection->original_ids(), $collection->ids()))) |
|
180
|
2 |
|
{ |
|
181
|
1 |
|
$query = $this->remove_items_query($model, $old_ids); |
|
182
|
|
|
if ($query) |
|
183
|
1 |
|
{ |
|
184
|
1 |
|
$query->execute(Jam::meta($this->model)->db()); |
|
185
|
1 |
|
} |
|
186
|
1 |
|
} |
|
187
|
|
|
|
|
188
|
2 |
|
if ($new_ids = array_values(array_diff($collection->ids(), $collection->original_ids()))) |
|
189
|
2 |
|
{ |
|
190
|
2 |
|
$query = $this->add_items_query($model, $new_ids); |
|
191
|
|
|
if ($query) |
|
192
|
2 |
|
{ |
|
193
|
2 |
|
$query->execute(Jam::meta($this->model)->db()); |
|
194
|
2 |
|
} |
|
195
|
2 |
|
} |
|
196
|
2 |
|
} |
|
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) |
|
204
|
|
|
{ |
|
205
|
1 |
|
if ($ids = array_filter($collection->ids())) |
|
206
|
1 |
|
{ |
|
207
|
1 |
|
$query = $this->remove_items_query($model, $ids); |
|
|
|
|
|
|
208
|
|
|
if ($query) |
|
209
|
1 |
|
{ |
|
210
|
1 |
|
$query->execute(Jam::meta($this->model)->db()); |
|
211
|
1 |
|
} |
|
212
|
1 |
|
} |
|
213
|
1 |
|
} |
|
214
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.