|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alpha\Model\Type; |
|
4
|
|
|
|
|
5
|
|
|
use Alpha\Util\Helper\Validator; |
|
6
|
|
|
use Alpha\Exception\IllegalArguementException; |
|
7
|
|
|
use Alpha\Model\ActiveRecord; |
|
8
|
|
|
use Alpha\Exception\FailedLookupCreateException; |
|
9
|
|
|
use ReflectionClass; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The Relation complex data type. |
|
13
|
|
|
* |
|
14
|
|
|
* @since 1.0 |
|
15
|
|
|
* |
|
16
|
|
|
* @author John Collins <[email protected]> |
|
17
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License |
|
18
|
|
|
* @copyright Copyright (c) 2017, John Collins (founder of Alpha Framework). |
|
19
|
|
|
* All rights reserved. |
|
20
|
|
|
* |
|
21
|
|
|
* <pre> |
|
22
|
|
|
* Redistribution and use in source and binary forms, with or |
|
23
|
|
|
* without modification, are permitted provided that the |
|
24
|
|
|
* following conditions are met: |
|
25
|
|
|
* |
|
26
|
|
|
* * Redistributions of source code must retain the above |
|
27
|
|
|
* copyright notice, this list of conditions and the |
|
28
|
|
|
* following disclaimer. |
|
29
|
|
|
* * Redistributions in binary form must reproduce the above |
|
30
|
|
|
* copyright notice, this list of conditions and the |
|
31
|
|
|
* following disclaimer in the documentation and/or other |
|
32
|
|
|
* materials provided with the distribution. |
|
33
|
|
|
* * Neither the name of the Alpha Framework nor the names |
|
34
|
|
|
* of its contributors may be used to endorse or promote |
|
35
|
|
|
* products derived from this software without specific |
|
36
|
|
|
* prior written permission. |
|
37
|
|
|
* |
|
38
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
|
39
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
|
40
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
|
41
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
42
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
|
43
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
44
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
45
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
46
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
47
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
48
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
|
49
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
50
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
51
|
|
|
* </pre> |
|
52
|
|
|
*/ |
|
53
|
|
|
class Relation extends Type implements TypeInterface |
|
54
|
|
|
{ |
|
55
|
|
|
/** |
|
56
|
|
|
* The name of the business object class which this class is related to. |
|
57
|
|
|
* |
|
58
|
|
|
* @var string |
|
59
|
|
|
* |
|
60
|
|
|
* @since 1.0 |
|
61
|
|
|
*/ |
|
62
|
|
|
private $relatedClass; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* The name of the fields of the business object class by which this class is related by. |
|
66
|
|
|
* |
|
67
|
|
|
* @var string |
|
68
|
|
|
* |
|
69
|
|
|
* @since 1.0 |
|
70
|
|
|
*/ |
|
71
|
|
|
private $relatedClassField; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* The name of the field from the related business object class which is displayed by the selection widget. |
|
75
|
|
|
* |
|
76
|
|
|
* @var string |
|
77
|
|
|
* |
|
78
|
|
|
* @since 1.0 |
|
79
|
|
|
*/ |
|
80
|
|
|
private $relatedClassDisplayField; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* An array of fields to use the values of while rendering related display values via the selection widget. |
|
84
|
|
|
* |
|
85
|
|
|
* @var array |
|
86
|
|
|
* |
|
87
|
|
|
* @since 1.0 |
|
88
|
|
|
*/ |
|
89
|
|
|
private $relatedClassHeaderFields = array(); |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* The name of the business object class on the left of a MANY-TO-MANY relation. |
|
93
|
|
|
* |
|
94
|
|
|
* @var string |
|
95
|
|
|
* |
|
96
|
|
|
* @since 1.0 |
|
97
|
|
|
*/ |
|
98
|
|
|
private $relatedClassLeft; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* The name of the field from the related business object class on the left of a |
|
102
|
|
|
* MANY-TO-MANY relation which is displayed by the selection widget. |
|
103
|
|
|
* |
|
104
|
|
|
* @var string |
|
105
|
|
|
* |
|
106
|
|
|
* @since 1.0 |
|
107
|
|
|
*/ |
|
108
|
|
|
private $relatedClassLeftDisplayField; |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* The name of the business object class on the right of a MANY-TO-MANY relation. |
|
112
|
|
|
* |
|
113
|
|
|
* @var string |
|
114
|
|
|
* |
|
115
|
|
|
* @since 1.0 |
|
116
|
|
|
*/ |
|
117
|
|
|
private $relatedClassRight; |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* The name of the field from the related business object class on the right of a |
|
121
|
|
|
* MANY-TO-MANY relation which is displayed by the selection widget. |
|
122
|
|
|
* |
|
123
|
|
|
* @var string |
|
124
|
|
|
* |
|
125
|
|
|
* @since 1.0 |
|
126
|
|
|
*/ |
|
127
|
|
|
private $relatedClassRightDisplayField; |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* The type of relation ('MANY-TO-ONE' or 'ONE-TO-MANY' or 'ONE-TO-ONE' or 'MANY-TO-MANY'). |
|
131
|
|
|
* |
|
132
|
|
|
* @var string |
|
133
|
|
|
* |
|
134
|
|
|
* @since 1.0 |
|
135
|
|
|
*/ |
|
136
|
|
|
private $relationType; |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* In the case of MANY-TO-MANY relationship, a lookup object will be required. |
|
140
|
|
|
* |
|
141
|
|
|
* @var \Alpha\Model\Type\RelationLookup |
|
142
|
|
|
* |
|
143
|
|
|
* @since 1.0 |
|
144
|
|
|
*/ |
|
145
|
|
|
private $lookup; |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* In the case of MANY-TO-MANY relationship, this transient array can be used to hold the IDs of related records. |
|
149
|
|
|
* |
|
150
|
|
|
* @var array |
|
151
|
|
|
* |
|
152
|
|
|
* @since 2.0 |
|
153
|
|
|
*/ |
|
154
|
|
|
private $IDs = array(); |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* When building a relation with the TagObject record, set this to the name of the tagged class. |
|
158
|
|
|
* |
|
159
|
|
|
* @var string |
|
160
|
|
|
* |
|
161
|
|
|
* @since 1.0 |
|
162
|
|
|
*/ |
|
163
|
|
|
private $taggedClass; |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* An array of the allowable relationship types ('MANY-TO-ONE' or 'ONE-TO-MANY' or 'ONE-TO-ONE' or 'MANY-TO-MANY'). |
|
167
|
|
|
* |
|
168
|
|
|
* @var array |
|
169
|
|
|
* |
|
170
|
|
|
* @since 1.0 |
|
171
|
|
|
*/ |
|
172
|
|
|
private $allowableRelationTypes = array('MANY-TO-ONE', 'ONE-TO-MANY', 'ONE-TO-ONE', 'MANY-TO-MANY'); |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* The ID value of the related object. In the special case of a MANY-TO-MANY |
|
176
|
|
|
* relation, contains the ID of the object on the current, accessing side. Can contain NULL. |
|
177
|
|
|
* |
|
178
|
|
|
* @var mixed |
|
179
|
|
|
* |
|
180
|
|
|
* @since 1.0 |
|
181
|
|
|
*/ |
|
182
|
|
|
private $value = null; |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* The validation rule for the Relation type. |
|
186
|
|
|
* |
|
187
|
|
|
* @var string |
|
188
|
|
|
* |
|
189
|
|
|
* @since 1.0 |
|
190
|
|
|
*/ |
|
191
|
|
|
private $validationRule; |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* The error message for the Relation type when validation fails. |
|
195
|
|
|
* |
|
196
|
|
|
* @var string |
|
197
|
|
|
* |
|
198
|
|
|
* @since 1.0 |
|
199
|
|
|
*/ |
|
200
|
|
|
protected $helper; |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* The size of the value for the this Relation. |
|
204
|
|
|
* |
|
205
|
|
|
* @var int |
|
206
|
|
|
* |
|
207
|
|
|
* @since 1.0 |
|
208
|
|
|
*/ |
|
209
|
|
|
private $size = 11; |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* The absolute maximum size of the value for the this Relation. |
|
213
|
|
|
* |
|
214
|
|
|
* @var int |
|
215
|
|
|
* |
|
216
|
|
|
* @since 1.0 |
|
217
|
|
|
*/ |
|
218
|
|
|
const MAX_SIZE = 11; |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Constructor. |
|
222
|
|
|
* |
|
223
|
|
|
* @since 1.0 |
|
224
|
|
|
*/ |
|
225
|
|
|
public function __construct() |
|
226
|
|
|
{ |
|
227
|
|
|
$this->validationRule = Validator::REQUIRED_INTEGER; |
|
228
|
|
|
$this->helper = ' not a valid Relation value! A maximum of '.$this->size.' characters is allowed.'; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Set the name of the business object class that this class is related to. |
|
233
|
|
|
* |
|
234
|
|
|
* @param string $RC |
|
235
|
|
|
* @param string $side Only required for MANY-TO-MANY relations |
|
236
|
|
|
* |
|
237
|
|
|
* @since 1.0 |
|
238
|
|
|
* |
|
239
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
|
240
|
|
|
*/ |
|
241
|
|
|
public function setRelatedClass($RC, $side = '') |
|
242
|
|
|
{ |
|
243
|
|
|
if (in_array($RC, ActiveRecord::getRecordClassNames())) { |
|
244
|
|
|
switch ($side) { |
|
245
|
|
|
case '': |
|
246
|
|
|
$this->relatedClass = $RC; |
|
247
|
|
|
break; |
|
248
|
|
|
case 'left': |
|
249
|
|
|
$this->relatedClassLeft = $RC; |
|
250
|
|
|
break; |
|
251
|
|
|
case 'right': |
|
252
|
|
|
$this->relatedClassRight = $RC; |
|
253
|
|
|
break; |
|
254
|
|
|
default: |
|
255
|
|
|
throw new IllegalArguementException('The side parameter ['.$RC.'] is not valid!'); |
|
256
|
|
|
} |
|
257
|
|
|
} else { |
|
258
|
|
|
throw new IllegalArguementException('The class ['.$RC.'] is not defined anywhere!'); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Get the name of the business object class that this class is related to. |
|
264
|
|
|
* |
|
265
|
|
|
* @param string $side |
|
266
|
|
|
* |
|
267
|
|
|
* @return string |
|
268
|
|
|
* |
|
269
|
|
|
* @since 1.0 |
|
270
|
|
|
* |
|
271
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
|
272
|
|
|
*/ |
|
273
|
|
|
public function getRelatedClass($side = '') |
|
274
|
|
|
{ |
|
275
|
|
|
switch ($side) { |
|
276
|
|
|
case '': |
|
277
|
|
|
return $this->relatedClass; |
|
278
|
|
|
break; |
|
|
|
|
|
|
279
|
|
|
case 'left': |
|
280
|
|
|
return $this->relatedClassLeft; |
|
281
|
|
|
break; |
|
|
|
|
|
|
282
|
|
|
case 'right': |
|
283
|
|
|
return $this->relatedClassRight; |
|
284
|
|
|
break; |
|
|
|
|
|
|
285
|
|
|
default: |
|
286
|
|
|
throw new IllegalArguementException('The side parameter ['.$side.'] is not valid!'); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Setter for the field of the related class. |
|
292
|
|
|
* |
|
293
|
|
|
* @param string $RCF |
|
294
|
|
|
* |
|
295
|
|
|
* @since 1.0 |
|
296
|
|
|
* |
|
297
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
|
298
|
|
|
*/ |
|
299
|
|
|
public function setRelatedClassField($RCF) |
|
300
|
|
|
{ |
|
301
|
|
|
// use reflection to sure the related class has the field $RCF |
|
302
|
|
|
$reflection = new ReflectionClass($this->relatedClass); |
|
303
|
|
|
$properties = $reflection->getProperties(); |
|
304
|
|
|
$fieldFound = false; |
|
305
|
|
|
|
|
306
|
|
|
foreach ($properties as $propObj) { |
|
307
|
|
|
if ($RCF == $propObj->name) { |
|
308
|
|
|
$fieldFound = true; |
|
309
|
|
|
break; |
|
310
|
|
|
} |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
if ($fieldFound) { |
|
314
|
|
|
$this->relatedClassField = $RCF; |
|
315
|
|
|
} else { |
|
316
|
|
|
throw new IllegalArguementException('The field ['.$RCF.'] was not found in the class ['.$this->relatedClass.']'); |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Getter for the field of the related class. |
|
322
|
|
|
* |
|
323
|
|
|
* @return string |
|
324
|
|
|
* |
|
325
|
|
|
* @since 1.0 |
|
326
|
|
|
*/ |
|
327
|
|
|
public function getRelatedClassField() |
|
328
|
|
|
{ |
|
329
|
|
|
return $this->relatedClassField; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Setter for ONE-TO-MANY relations, which sets the header fields to |
|
334
|
|
|
* render from the related class. |
|
335
|
|
|
* |
|
336
|
|
|
* @param array $fieldNames |
|
337
|
|
|
* |
|
338
|
|
|
* @since 1.0 |
|
339
|
|
|
*/ |
|
340
|
|
|
public function setRelatedClassHeaderFields($fieldNames) |
|
341
|
|
|
{ |
|
342
|
|
|
$this->relatedClassHeaderFields = $fieldNames; |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Getter for the selection widget field headings of the related class. |
|
347
|
|
|
* |
|
348
|
|
|
* @return array |
|
349
|
|
|
* |
|
350
|
|
|
* @since 1.0 |
|
351
|
|
|
*/ |
|
352
|
|
|
public function getRelatedClassHeaderFields() |
|
353
|
|
|
{ |
|
354
|
|
|
return $this->relatedClassHeaderFields; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Setter for the display field from the related class. |
|
359
|
|
|
* |
|
360
|
|
|
* @param string $RCDF |
|
361
|
|
|
* @param string $side Only required for MANY-TO-MANY relations |
|
362
|
|
|
* |
|
363
|
|
|
* @since 1.0 |
|
364
|
|
|
* |
|
365
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
|
366
|
|
|
*/ |
|
367
|
|
|
public function setRelatedClassDisplayField($RCDF, $side = '') |
|
368
|
|
|
{ |
|
369
|
|
|
switch ($side) { |
|
370
|
|
|
case '': |
|
371
|
|
|
$this->relatedClassDisplayField = $RCDF; |
|
372
|
|
|
break; |
|
373
|
|
|
case 'left': |
|
374
|
|
|
$this->relatedClassLeftDisplayField = $RCDF; |
|
375
|
|
|
break; |
|
376
|
|
|
case 'right': |
|
377
|
|
|
$this->relatedClassRightDisplayField = $RCDF; |
|
378
|
|
|
break; |
|
379
|
|
|
default: |
|
380
|
|
|
throw new IllegalArguementException('The side paramter ['.$RC.'] is not valid!'); |
|
381
|
|
|
} |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
/** |
|
385
|
|
|
* Getter for the display field from the related class. |
|
386
|
|
|
* |
|
387
|
|
|
* @param string $side Only required for MANY-TO-MANY relations |
|
388
|
|
|
* |
|
389
|
|
|
* @return string |
|
390
|
|
|
* |
|
391
|
|
|
* @since 1.0 |
|
392
|
|
|
* |
|
393
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
|
394
|
|
|
*/ |
|
395
|
|
|
public function getRelatedClassDisplayField($side = '') |
|
396
|
|
|
{ |
|
397
|
|
|
switch ($side) { |
|
398
|
|
|
case '': |
|
399
|
|
|
return $this->relatedClassDisplayField; |
|
400
|
|
|
break; |
|
|
|
|
|
|
401
|
|
|
case 'left': |
|
402
|
|
|
return $this->relatedClassLeftDisplayField; |
|
403
|
|
|
break; |
|
|
|
|
|
|
404
|
|
|
case 'right': |
|
405
|
|
|
return $this->relatedClassRightDisplayField; |
|
406
|
|
|
break; |
|
|
|
|
|
|
407
|
|
|
default: |
|
408
|
|
|
throw new IllegalArguementException('The side paramter ['.$RC.'] is not valid!'); |
|
409
|
|
|
} |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
/** |
|
413
|
|
|
* Setter for the relation type. |
|
414
|
|
|
* |
|
415
|
|
|
* @param string $RT |
|
416
|
|
|
* |
|
417
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
|
418
|
|
|
* @throws \Alpha\Exception\FailedLookupCreateException |
|
419
|
|
|
* |
|
420
|
|
|
* @since 1.0 |
|
421
|
|
|
*/ |
|
422
|
|
|
public function setRelationType($RT) |
|
423
|
|
|
{ |
|
424
|
|
|
if (in_array($RT, $this->allowableRelationTypes)) { |
|
425
|
|
|
$this->relationType = $RT; |
|
426
|
|
|
if ($RT == 'MANY-TO-MANY') { |
|
427
|
|
|
try { |
|
428
|
|
|
$this->lookup = new RelationLookup($this->relatedClassLeft, $this->relatedClassRight); |
|
429
|
|
|
} catch (FailedLookupCreateException $flce) { |
|
430
|
|
|
throw $flce; |
|
431
|
|
|
} catch (IllegalArguementException $iae) { |
|
432
|
|
|
throw $iae; |
|
433
|
|
|
} |
|
434
|
|
|
} |
|
435
|
|
|
} else { |
|
436
|
|
|
throw new IllegalArguementException('Relation type of ['.$RT.'] is invalid!'); |
|
437
|
|
|
} |
|
438
|
|
|
} |
|
439
|
|
|
|
|
440
|
|
|
/** |
|
441
|
|
|
* Getter for the relation type. |
|
442
|
|
|
* |
|
443
|
|
|
* @return string |
|
444
|
|
|
* |
|
445
|
|
|
* @since 1.0 |
|
446
|
|
|
*/ |
|
447
|
|
|
public function getRelationType() |
|
448
|
|
|
{ |
|
449
|
|
|
return $this->relationType; |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
/** |
|
453
|
|
|
* Setter for the value ID of this relation. |
|
454
|
|
|
* |
|
455
|
|
|
* @param int $val |
|
456
|
|
|
* |
|
457
|
|
|
* @since 1.0 |
|
458
|
|
|
* |
|
459
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
|
460
|
|
|
*/ |
|
461
|
|
|
public function setValue($val) |
|
462
|
|
|
{ |
|
463
|
|
|
if (empty($val)) { |
|
464
|
|
|
$this->value = null; |
|
465
|
|
|
} else { |
|
466
|
|
|
if (!Validator::isInteger($val)) { |
|
467
|
|
|
throw new IllegalArguementException("[$val]".$this->helper); |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
|
|
if (mb_strlen($val) <= $this->size) { |
|
471
|
|
|
$this->value = str_pad($val, 11, '0', STR_PAD_LEFT); |
|
472
|
|
|
} else { |
|
473
|
|
|
throw new IllegalArguementException("[$val]".$this->helper); |
|
474
|
|
|
} |
|
475
|
|
|
} |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
/** |
|
479
|
|
|
* Getter for the array of IDs used by MANY-TO-MANY instances. |
|
480
|
|
|
* |
|
481
|
|
|
* @return array |
|
482
|
|
|
* |
|
483
|
|
|
* @since 2.0 |
|
484
|
|
|
*/ |
|
485
|
|
|
public function getRelatedIDs() |
|
486
|
|
|
{ |
|
487
|
|
|
return $this->IDs; |
|
488
|
|
|
} |
|
489
|
|
|
|
|
490
|
|
|
/** |
|
491
|
|
|
* Setter for the array of IDs used by MANY-TO-MANY instances. |
|
492
|
|
|
* |
|
493
|
|
|
* @param array $IDs |
|
494
|
|
|
* |
|
495
|
|
|
* @since 2.0 |
|
496
|
|
|
* |
|
497
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
|
498
|
|
|
*/ |
|
499
|
|
|
public function setRelatedIDs($IDs) |
|
500
|
|
|
{ |
|
501
|
|
|
if (is_array($IDs)) { |
|
502
|
|
|
$this->IDs = $IDs; |
|
503
|
|
|
} else { |
|
504
|
|
|
throw new IllegalArguementException('An array must be provided to setRelatedIDs()!'); |
|
505
|
|
|
} |
|
506
|
|
|
} |
|
507
|
|
|
|
|
508
|
|
|
/** |
|
509
|
|
|
* Getter for the Relation value. |
|
510
|
|
|
* |
|
511
|
|
|
* @return string |
|
512
|
|
|
* |
|
513
|
|
|
* @since 1.0 |
|
514
|
|
|
*/ |
|
515
|
|
|
public function getValue() |
|
516
|
|
|
{ |
|
517
|
|
|
return $this->value; |
|
518
|
|
|
} |
|
519
|
|
|
|
|
520
|
|
|
/** |
|
521
|
|
|
* Get the validation rule. |
|
522
|
|
|
* |
|
523
|
|
|
* @return string |
|
524
|
|
|
* |
|
525
|
|
|
* @since 1.0 |
|
526
|
|
|
*/ |
|
527
|
|
|
public function getRule() |
|
528
|
|
|
{ |
|
529
|
|
|
return $this->validationRule; |
|
530
|
|
|
} |
|
531
|
|
|
|
|
532
|
|
|
/** |
|
533
|
|
|
* Setter to override the default validation rule. |
|
534
|
|
|
* |
|
535
|
|
|
* @param string $rule |
|
536
|
|
|
* |
|
537
|
|
|
* @since 1.0 |
|
538
|
|
|
*/ |
|
539
|
|
|
public function setRule($rule) |
|
540
|
|
|
{ |
|
541
|
|
|
$this->validationRule = $rule; |
|
542
|
|
|
} |
|
543
|
|
|
|
|
544
|
|
|
/** |
|
545
|
|
|
* Getter for the display value of the related class field. In the case of a |
|
546
|
|
|
* MANY-TO-MANY Relation, a comma-seperated sorted list of values is returned. |
|
547
|
|
|
* |
|
548
|
|
|
* @param string $accessingClassName Used to indicate the reading side when accessing from MANY-TO-MANY relation (leave blank for other relation types) |
|
549
|
|
|
* |
|
550
|
|
|
* @return string |
|
551
|
|
|
* |
|
552
|
|
|
* @since 1.0 |
|
553
|
|
|
* |
|
554
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
|
555
|
|
|
*/ |
|
556
|
|
|
public function getRelatedClassDisplayFieldValue($accessingClassName = '') |
|
557
|
|
|
{ |
|
558
|
|
|
if ($this->relationType == 'MANY-TO-MANY') { |
|
559
|
|
|
/* |
|
560
|
|
|
* 1. Use RelationLookup to get IDs of related objects |
|
561
|
|
|
* 2. Load related objects |
|
562
|
|
|
* 3. Access the value of the field on the object to build the |
|
563
|
|
|
* comma-seperated list. |
|
564
|
|
|
*/ |
|
565
|
|
|
if (empty($this->lookup)) { |
|
566
|
|
|
throw new IllegalArguementException('Tried to load related MANY-TO-MANY fields but no RelationLookup set on the Relation object!'); |
|
567
|
|
|
} |
|
568
|
|
|
|
|
569
|
|
|
if (empty($accessingClassName)) { |
|
570
|
|
|
throw new IllegalArguementException('Tried to load related MANY-TO-MANY fields but no accessingClassName parameter set on the call to getRelatedClassDisplayFieldValue!'); |
|
571
|
|
|
} |
|
572
|
|
|
|
|
573
|
|
|
// load objects on the right from accessing on the left |
|
574
|
|
|
if ($accessingClassName == $this->relatedClassLeft) { |
|
575
|
|
|
$obj = new $this->relatedClassRight(); |
|
576
|
|
|
|
|
577
|
|
|
$lookupObjects = $this->lookup->loadAllByAttribute('leftID', $this->value); |
|
578
|
|
|
|
|
579
|
|
|
$values = array(); |
|
580
|
|
|
foreach ($lookupObjects as $lookupObject) { |
|
581
|
|
|
$obj->load($lookupObject->get('rightID')); |
|
582
|
|
|
array_push($values, $obj->get($this->relatedClassRightDisplayField)); |
|
583
|
|
|
} |
|
584
|
|
|
// sort array, then return as comma-seperated string |
|
585
|
|
|
asort($values); |
|
586
|
|
|
|
|
587
|
|
|
return implode(',', $values); |
|
588
|
|
|
} |
|
589
|
|
|
// load objects on the left from accessing on the right |
|
590
|
|
|
if ($accessingClassName == $this->relatedClassRight) { |
|
591
|
|
|
$obj = new $this->relatedClassLeft(); |
|
592
|
|
|
|
|
593
|
|
|
$lookupObjects = $this->lookup->loadAllByAttribute('rightID', $this->value); |
|
594
|
|
|
|
|
595
|
|
|
$values = array(); |
|
596
|
|
|
foreach ($lookupObjects as $lookupObject) { |
|
597
|
|
|
$obj->load($lookupObject->get('leftID')); |
|
598
|
|
|
array_push($values, $obj->get($this->relatedClassLeftDisplayField)); |
|
599
|
|
|
} |
|
600
|
|
|
// sort array, then return as comma-seperated string |
|
601
|
|
|
asort($values); |
|
602
|
|
|
|
|
603
|
|
|
return implode(',', $values); |
|
604
|
|
|
} |
|
605
|
|
|
} else { |
|
606
|
|
|
$obj = new $this->relatedClass(); |
|
607
|
|
|
// making sure we have an object to load |
|
608
|
|
|
if (empty($this->value) || $this->value == '00000000000') { |
|
609
|
|
|
return ''; |
|
610
|
|
|
} else { |
|
611
|
|
|
$obj->load($this->value); |
|
612
|
|
|
|
|
613
|
|
|
return $obj->get($this->relatedClassDisplayField); |
|
614
|
|
|
} |
|
615
|
|
|
} |
|
616
|
|
|
} |
|
617
|
|
|
|
|
618
|
|
|
/** |
|
619
|
|
|
* For one-to-many and many-to-many relations, get an array of records on the other side. For one-to-one |
|
620
|
|
|
* relations, get the record (Alpha\Model\ActiveRecord instance) on the other side. |
|
621
|
|
|
* |
|
622
|
|
|
* string $accessingClassName Used to indicate the reading side when accessing from MANY-TO-MANY relation (leave blank for other relation types) |
|
623
|
|
|
* |
|
624
|
|
|
* @return array|Alpha\Model\ActiveRecord |
|
625
|
|
|
* |
|
626
|
|
|
* @since 3.0 |
|
627
|
|
|
* |
|
628
|
|
|
* @throws \Alpha\Exception\IllegalArguementException |
|
629
|
|
|
*/ |
|
630
|
|
|
public function getRelated($accessingClassName = '') |
|
631
|
|
|
{ |
|
632
|
|
|
if ($this->relationType == 'ONE-TO-ONE') { |
|
633
|
|
|
if (!class_exists($this->relatedClass)) { |
|
634
|
|
|
throw new IllegalArguementException('Could not load the definition for the ActiveRecord class ['.$this->relatedClass.']'); |
|
635
|
|
|
} |
|
636
|
|
|
|
|
637
|
|
|
$record = new $this->relatedClass(); |
|
638
|
|
|
$record->loadByAttribute($this->getRelatedClassField(), $this->getValue()); |
|
639
|
|
|
|
|
640
|
|
|
return $record; |
|
641
|
|
|
} |
|
642
|
|
|
|
|
643
|
|
|
if ($this->relationType == 'ONE-TO-MANY') { |
|
644
|
|
|
if ($this->getValue() == '') { // if the value is empty, then return an empty array |
|
645
|
|
|
return array(); |
|
646
|
|
|
} |
|
647
|
|
|
|
|
648
|
|
|
$record = new $this->relatedClass(); |
|
649
|
|
|
if ($this->relatedClass == 'Alpha\Model\Tag') { |
|
650
|
|
|
$records = $record->loadTags($this->taggedClass, $this->getValue()); |
|
651
|
|
|
} else { |
|
652
|
|
|
$records = $record->loadAllByAttribute($this->getRelatedClassField(), $this->getValue()); |
|
653
|
|
|
} |
|
654
|
|
|
|
|
655
|
|
|
return $records; |
|
656
|
|
|
} else { // MANY-TO-MANY |
|
657
|
|
|
if (empty($this->lookup)) { |
|
658
|
|
|
throw new IllegalArguementException('Tried to load related MANY-TO-MANY objects but no RelationLookup set on the Relation object!'); |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
if (empty($accessingClassName)) { |
|
662
|
|
|
throw new IllegalArguementException('Tried to load related MANY-TO-MANY objects but no accessingClassName parameter set on the call to getRelated!'); |
|
663
|
|
|
} |
|
664
|
|
|
|
|
665
|
|
|
$records = array(); |
|
666
|
|
|
|
|
667
|
|
|
// load objects on the right from accessing on the left |
|
668
|
|
|
if ($accessingClassName == $this->relatedClassLeft) { |
|
669
|
|
|
$lookups = $this->lookup->loadAllByAttribute('leftID', $this->value); |
|
670
|
|
|
|
|
671
|
|
|
foreach ($lookups as $lookup) { |
|
672
|
|
|
$record = new $this->relatedClassRight(); |
|
673
|
|
|
$record->load($lookup->get('rightID')); |
|
674
|
|
|
array_push($records, $record); |
|
675
|
|
|
} |
|
676
|
|
|
} |
|
677
|
|
|
// load objects on the left from accessing on the right |
|
678
|
|
|
if ($accessingClassName == $this->relatedClassRight && count($records) == 0) { |
|
679
|
|
|
$lookups = $this->lookup->loadAllByAttribute('rightID', $this->value); |
|
680
|
|
|
|
|
681
|
|
|
foreach ($lookups as $lookup) { |
|
682
|
|
|
$record = new $this->relatedClassLeft(); |
|
683
|
|
|
$record->load($lookup->get('leftID')); |
|
684
|
|
|
array_push($records, $record); |
|
685
|
|
|
} |
|
686
|
|
|
} |
|
687
|
|
|
|
|
688
|
|
|
return $records; |
|
689
|
|
|
} |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
/** |
|
693
|
|
|
* Get the allowable size of the Relation in the database field. |
|
694
|
|
|
* |
|
695
|
|
|
* @return int |
|
696
|
|
|
* |
|
697
|
|
|
* @since 1.0 |
|
698
|
|
|
*/ |
|
699
|
|
|
public function getSize() |
|
700
|
|
|
{ |
|
701
|
|
|
return $this->size; |
|
702
|
|
|
} |
|
703
|
|
|
|
|
704
|
|
|
/** |
|
705
|
|
|
* Get the lookup object if available (only on MANY-TO-MANY relations, null otherwise). |
|
706
|
|
|
* |
|
707
|
|
|
* @return RelationLookup |
|
708
|
|
|
* |
|
709
|
|
|
* @since 1.0 |
|
710
|
|
|
*/ |
|
711
|
|
|
public function getLookup() |
|
712
|
|
|
{ |
|
713
|
|
|
return $this->lookup; |
|
714
|
|
|
} |
|
715
|
|
|
|
|
716
|
|
|
/** |
|
717
|
|
|
* Gets the side ('left' or 'right') of the passed classname on the current Relation object. |
|
718
|
|
|
* |
|
719
|
|
|
* @param string $RecordClassname |
|
720
|
|
|
* |
|
721
|
|
|
* @return string |
|
722
|
|
|
* |
|
723
|
|
|
* @since 1.0 |
|
724
|
|
|
* |
|
725
|
|
|
* @throws \Alpha\Model\Type\IllegalArguementException |
|
726
|
|
|
*/ |
|
727
|
|
|
public function getSide($RecordClassname) |
|
728
|
|
|
{ |
|
729
|
|
|
if ($RecordClassname == $this->relatedClassLeft) { |
|
730
|
|
|
return 'left'; |
|
731
|
|
|
} elseif ($RecordClassname == $this->relatedClassRight) { |
|
732
|
|
|
return 'right'; |
|
733
|
|
|
} else { |
|
734
|
|
|
throw new IllegalArguementException('Error trying to determine the MANY-TO-MANY relationship side for the classname ['.$RecordClassname.']'); |
|
735
|
|
|
} |
|
736
|
|
|
} |
|
737
|
|
|
|
|
738
|
|
|
/** |
|
739
|
|
|
* Set the taggedClass property to the name of the tagged class when building relations |
|
740
|
|
|
* to the TagObject record. |
|
741
|
|
|
* |
|
742
|
|
|
* @param string $taggedClass |
|
743
|
|
|
* |
|
744
|
|
|
* @since 1.0 |
|
745
|
|
|
*/ |
|
746
|
|
|
public function setTaggedClass($taggedClass) |
|
747
|
|
|
{ |
|
748
|
|
|
$this->taggedClass = $taggedClass; |
|
749
|
|
|
} |
|
750
|
|
|
} |
|
751
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.