|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace As3\Modlr\Models\Collections; |
|
4
|
|
|
|
|
5
|
|
|
use As3\Modlr\Metadata\EntityMetadata; |
|
6
|
|
|
use As3\Modlr\Models\AbstractModel; |
|
7
|
|
|
use As3\Modlr\Models\Model; |
|
8
|
|
|
use As3\Modlr\Store\Store; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Model collection that contains record representations from a persistence (database) layer. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Jacob Bare <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class InverseCollection extends ModelCollection |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* The owning model of the inverse relationship. |
|
19
|
|
|
* |
|
20
|
|
|
* @var Model |
|
21
|
|
|
*/ |
|
22
|
|
|
private $owner; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The inverse relationship query field. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $inverseField; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritDoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $loaded = false; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritDoc} |
|
38
|
|
|
* @param Model $owner |
|
39
|
|
|
* @param string $inverseField |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(EntityMetadata $metadata, Store $store, Model $owner, $inverseField) |
|
42
|
|
|
{ |
|
43
|
|
|
parent::__construct($metadata, $store, [], 0); |
|
44
|
|
|
$this->owner = $owner; |
|
45
|
|
|
$this->inverseField = $inverseField; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
* |
|
51
|
|
|
* Overwritten to prevent modification. |
|
52
|
|
|
* |
|
53
|
|
|
* @throws \BadMethodCallException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function clear() |
|
56
|
|
|
{ |
|
57
|
|
|
throw new \BadMethodCallException('You cannot clear inverse collections.'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritDoc} |
|
62
|
|
|
* |
|
63
|
|
|
* Overwritten to ensure the collection is loaded, since references aren't sent to inverse collections. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function count() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->loadFromStore(); |
|
68
|
|
|
return parent::count(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritDoc} |
|
73
|
|
|
* |
|
74
|
|
|
* Overwritten to ensure the collection is loaded, since references aren't sent to inverse collections. |
|
75
|
|
|
*/ |
|
76
|
|
|
public function allWithoutLoad() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->loadFromStore(); |
|
79
|
|
|
return parent::allWithoutLoad(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritDoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getIdentifiers($onlyUnloaded = true) |
|
86
|
|
|
{ |
|
87
|
|
|
if (true === $this->loaded) { |
|
88
|
|
|
return []; |
|
89
|
|
|
} |
|
90
|
|
|
return [$this->owner->getId()]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Gets the model that owns this inverse collection. |
|
95
|
|
|
* |
|
96
|
|
|
* @return Model |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getOwner() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->owner; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritDoc} |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getQueryField() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->inverseField; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritDoc} |
|
113
|
|
|
* |
|
114
|
|
|
* Overwritten to ensure the collection is loaded, since references aren't sent to inverse collections. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getTotalCount() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->count(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritDoc} |
|
123
|
|
|
* |
|
124
|
|
|
* Overwritten to always return false. |
|
125
|
|
|
* |
|
126
|
|
|
*/ |
|
127
|
|
|
public function isDirty() |
|
128
|
|
|
{ |
|
129
|
|
|
return false; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
* |
|
135
|
|
|
* Overwritten to prevent modification. |
|
136
|
|
|
* |
|
137
|
|
|
* @throws \BadMethodCallException |
|
138
|
|
|
*/ |
|
139
|
|
|
public function push(AbstractModel $model) |
|
140
|
|
|
{ |
|
141
|
|
|
throw new \BadMethodCallException('You cannot push to inverse collections.'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* {@inheritdoc} |
|
146
|
|
|
* |
|
147
|
|
|
* Overwritten to prevent modification. |
|
148
|
|
|
* |
|
149
|
|
|
* @throws \BadMethodCallException |
|
150
|
|
|
*/ |
|
151
|
|
|
public function remove(AbstractModel $model) |
|
152
|
|
|
{ |
|
153
|
|
|
throw new \BadMethodCallException('You cannot remove from an inverse collections.'); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|