/
UnsavedRelationList.php
432 lines (368 loc) · 11 KB
/
UnsavedRelationList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
/**
* An ArrayList that represents an unsaved relation.
*
* has_many and many_many relations cannot be saved until after the DataObject they're
* on has been written. This List pretends to be a RelationList and stores the related
* objects in memory.
*
* It can store both saved objects (as IDs) or unsaved objects (as instances of
* $dataClass). Unsaved objects are then written when the list is saved into an instance
* of RelationList.
*
* Most methods that alter the list of objects throw LogicExceptions.
*/
class UnsavedRelationList extends ArrayList {
/**
* The DataObject class name that this relation is on
*
* @var string
*/
protected $baseClass;
/**
* The name of the relation
*
* @var string
*/
protected $relationName;
/**
* The DataObject class name that this relation is querying
*
* @var string
*/
protected $dataClass;
/**
* The extra fields associated with the relation
*
* @var array
*/
protected $extraFields = array();
/**
* Create a new UnsavedRelationList
*
* @param string $dataClass The DataObject class used in the relation
*/
public function __construct($baseClass, $relationName, $dataClass) {
$this->baseClass = $baseClass;
$this->relationName = $relationName;
$this->dataClass = $dataClass;
}
/**
* Add an item to this relationship
*
* @param $extraFields A map of additional columns to insert into the joinTable in the case of a many_many relation
*/
public function add($item, $extraFields = null) {
$this->push($item, $extraFields);
}
/**
* Save all the items in this list into the RelationList
*
* @param RelationList $list
*/
public function changeToList(RelationList $list) {
foreach($this->items as $key => $item) {
if(is_object($item)) {
$item->write();
}
$list->add($item, $this->extraFields[$key]);
}
}
/**
* Pushes an item onto the end of this list.
*
* @param array|object $item
*/
public function push($item, $extraFields = null) {
if((is_object($item) && !$item instanceof $this->dataClass)
|| (!is_object($item) && !is_numeric($item))) {
throw new InvalidArgumentException(
"UnsavedRelationList::add() expecting a $this->dataClass object, or ID value",
E_USER_ERROR);
}
if(is_object($item) && $item->ID) {
$item = $item->ID;
}
$this->extraFields[] = $extraFields;
parent::push($item);
}
/**
* Get the dataClass name for this relation, ie the DataObject ClassName
*
* @return string
*/
public function dataClass() {
return $this->dataClass;
}
/**
* Returns an Iterator for this relation.
*
* @return ArrayIterator
*/
public function getIterator() {
return new ArrayIterator($this->toArray());
}
/**
* Return an array of the actual items that this relation contains at this stage.
* This is when the query is actually executed.
*
* @return array
*/
public function toArray() {
$items = array();
foreach($this->items as $key => $item) {
if(is_numeric($item)) {
$item = DataObject::get_by_id($this->dataClass, $item);
}
if(!empty($this->extraFields[$key])) {
$item->update($this->extraFields[$key]);
}
$items[] = $item;
}
return $items;
}
/**
* Add a number of items to the relation.
*
* @param array $items Items to add, as either DataObjects or IDs.
* @return DataList
*/
public function addMany($items) {
foreach($items as $item) {
$this->add($item);
}
return $this;
}
/**
* Returns true if the given column can be used to filter the records.
*/
public function canFilterBy($by) {
return false;
}
/**
* Returns true if the given column can be used to sort the records.
*/
public function canSortBy($by) {
return false;
}
/**
* Remove all items from this relation.
*/
public function removeAll() {
$this->items = array();
$this->extraFields = array();
}
/**
* Remove the items from this list with the given IDs
*
* @param array $idList
*/
public function removeMany($items) {
$this->items = array_diff($this->items, $items);
return $this;
}
/**
* Removes items from this list which are equal.
*
* @param string $field unused
*/
public function removeDuplicates($field = 'ID') {
$this->items = array_unique($this->items);
}
/**
* Sets the Relation to be the given ID list.
* Records will be added and deleted as appropriate.
*
* @param array $idList List of IDs.
*/
public function setByIDList($idList) {
$this->removeAll();
$this->addMany($idList);
}
/**
* Returns the first item in the list
*
* @return mixed
*/
public function first() {
$item = reset($this->items);
if(is_numeric($item)) {
$item = DataObject::get_by_id($this->dataClass, $item);
}
if(!empty($this->extraFields[key($this->items)])) {
$item->update($this->extraFields[key($this->items)]);
}
return $item;
}
/**
* Returns the last item in the list
*
* @return mixed
*/
public function last() {
$item = end($this->items);
if(!empty($this->extraFields[key($this->items)])) {
$item->update($this->extraFields[key($this->items)]);
}
return $item;
}
/**
* Returns an array of a single field value for all items in the list.
*
* @param string $colName
* @return array
*/
public function column($colName = 'ID') {
$list = new ArrayList($this->toArray());
return $list->column('ID');
}
/**
* Set the ID of the record that this RelationList is linking.
*
* Adds the
*
* @param $id A single ID, or an array of IDs
*/
public function setForeignID($id) {
$class = singleton($this->baseClass);
$class->ID = 1;
return $class->{$this->relationName}()->setForeignID($id);
}
/**
* Returns a copy of this list with the relationship linked to the given foreign ID.
* @param $id An ID or an array of IDs.
*/
public function forForeignID($id) {
return $this->setForeignID($id);
}
/**
* Return the DBField object that represents the given field on the related class.
*
* @param string $fieldName Name of the field
* @return DBField The field as a DBField object
*/
public function dbObject($fieldName) {
return singleton($this->dataClass)->dbObject($fieldName);
}
/**#@+
* Prevents calling DataList methods that rely on the objects being saved
*/
public function addFilter() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function alterDataQuery() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function avg() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function byIDs() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function byID($id) {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function dataQuery() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function exclude() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function filter() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function getIDList() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function getRange($offset, $length) {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function getRelationName() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function innerJoin() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function insertFirst() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function join() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function leftJoin() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function limit($length, $offset = 0) {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function map($keyField = 'ID', $titleField = 'Title') {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function max() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function merge($with) {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function min() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function newObject() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function offsetExists($offset) {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function offsetGet($offset) {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function offsetSet($offset, $value) {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function offsetUnset($offset) {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function pop() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function relation() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function removeByFilter() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function removeByID() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function reverse() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function setDataModel() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function setDataQuery() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function setQueriedColumns() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function shift() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function sql() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function subtract() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function sum() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function unshift($item) {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
public function where() {
throw new LogicException(__FUNCTION__ . " can't be called on an UnsavedRelationList.");
}
/**#@-*/
}