1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleMapper; |
4
|
|
|
|
5
|
|
|
use Nette\Database\Table\Selection as NetteDatabaseSelection; |
6
|
|
|
use Nette\Database\Table\ActiveRow as NetteDatabaseActiveRow; |
7
|
|
|
use ArrayAccess; |
8
|
|
|
use ArrayIterator; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
use Nette\Database\Table\IRow; |
11
|
|
|
|
12
|
|
|
class ActiveRow implements ArrayAccess, IteratorAggregate |
13
|
|
|
{ |
14
|
|
|
/** @var NetteDatabaseActiveRow */ |
15
|
|
|
protected $record; |
16
|
|
|
|
17
|
|
|
/** @var Structure */ |
18
|
|
|
protected $structure; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param NetteDatabaseActiveRow $record |
22
|
|
|
* @param Structure $structure |
23
|
|
|
*/ |
24
|
36 |
|
public function __construct(NetteDatabaseActiveRow $record, Structure $structure) |
25
|
|
|
{ |
26
|
36 |
|
$this->record = $record; |
27
|
36 |
|
$this->structure = $structure; |
28
|
36 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $name |
32
|
|
|
* @return mixed|NetteDatabaseActiveRow |
33
|
|
|
*/ |
34
|
12 |
|
public function __get($name) |
35
|
|
|
{ |
36
|
|
|
// Try to find record property |
37
|
12 |
|
return $this->record->$name; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return NetteDatabaseActiveRow |
42
|
|
|
*/ |
43
|
|
|
public function getRecord() |
44
|
|
|
{ |
45
|
|
|
return $this->record; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/**********************************************************************\ |
49
|
|
|
* Wrapper function |
50
|
|
|
\**********************************************************************/ |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function __toString() |
56
|
|
|
{ |
57
|
|
|
return (string) $this->record; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
6 |
|
public function toArray() |
64
|
|
|
{ |
65
|
6 |
|
return $this->record->toArray(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param bool|true $need |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
2 |
|
public function getPrimary($need = true) |
73
|
|
|
{ |
74
|
2 |
|
return $this->record->getPrimary($need); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param bool|true $need |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
2 |
|
public function getSignature($need = true) |
82
|
|
|
{ |
83
|
2 |
|
return $this->record->getSignature($need); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array|\Traversable $data |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
2 |
|
public function update($data) |
91
|
|
|
{ |
92
|
2 |
|
return $this->record->update($data); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
2 |
|
public function delete() |
99
|
|
|
{ |
100
|
2 |
|
return $this->record->delete(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns referenced row |
105
|
|
|
* @param string $key |
106
|
|
|
* @param string $throughColumn |
107
|
|
|
* @return IRow|null |
108
|
|
|
*/ |
109
|
6 |
|
public function ref($key, $throughColumn = null) |
110
|
|
|
{ |
111
|
6 |
|
$row = $this->record->ref($key, $throughColumn); |
112
|
6 |
|
return $row instanceof IRow ? $this->prepareRecord($row) : $row; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns referencing rows |
118
|
|
|
* @param string $key |
119
|
|
|
* @param string $throughColumn |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
4 |
|
public function related($key, $throughColumn = null) |
123
|
|
|
{ |
124
|
4 |
|
$selection = $this->record->related($key, $throughColumn); |
125
|
4 |
|
return $selection instanceof NetteDatabaseSelection ? $this->prepareSelection($selection) : $selection; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/**********************************************************************\ |
129
|
|
|
* IteratorAggregate interface |
130
|
|
|
\**********************************************************************/ |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return ArrayIterator |
134
|
|
|
*/ |
135
|
2 |
|
public function getIterator() |
136
|
|
|
{ |
137
|
2 |
|
return $this->record->getIterator(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/**********************************************************************\ |
141
|
|
|
* ArrayAccess interface |
142
|
|
|
\**********************************************************************/ |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Stores value in column |
146
|
|
|
* @param string $key column name |
147
|
|
|
* @param string $value |
148
|
|
|
*/ |
149
|
2 |
|
public function offsetSet($key, $value) |
150
|
|
|
{ |
151
|
2 |
|
$this->record->offsetSet($key, $value); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns value of column |
156
|
|
|
* @param string $key column name |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
2 |
|
public function offsetGet($key) |
160
|
|
|
{ |
161
|
2 |
|
return $this->record->offsetGet($key); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Tests if column exists |
166
|
|
|
* @param string $key column name |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
2 |
|
public function offsetExists($key) |
170
|
|
|
{ |
171
|
2 |
|
return $this->record->offsetExists($key); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Removes column from data |
176
|
|
|
* @param string $key column name |
177
|
|
|
*/ |
178
|
2 |
|
public function offsetUnset($key) |
179
|
|
|
{ |
180
|
2 |
|
$this->record->offsetUnset($key); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/**********************************************************************\ |
184
|
|
|
* Extend methods |
185
|
|
|
\**********************************************************************/ |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns mm referencing rows |
189
|
|
|
* @param Selection $selection |
190
|
|
|
* @param string $ref |
191
|
|
|
* @param string $refPrimary |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
4 |
|
protected function mmRelated(Selection $selection, $ref, $refPrimary = 'id') |
195
|
|
|
{ |
196
|
4 |
|
$result = []; |
197
|
4 |
|
foreach ($selection as $row) { |
198
|
4 |
|
$result[$row->ref($ref)->$refPrimary] = $row->ref($ref); |
199
|
4 |
|
} |
200
|
4 |
|
return $result; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/**********************************************************************\ |
204
|
|
|
* Build methods |
205
|
|
|
\**********************************************************************/ |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Prepare one record |
209
|
|
|
* @param IRow $row |
210
|
|
|
* @return mixed |
211
|
|
|
*/ |
212
|
6 |
|
protected function prepareRecord(IRow $row) |
213
|
|
|
{ |
214
|
6 |
|
$recordClass = $this->structure->getActiveRowClass($row->getTable()->getName()); |
215
|
6 |
|
return new $recordClass($row, $this->structure); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Prepare selection |
220
|
|
|
* @param NetteDatabaseSelection $selection |
221
|
|
|
* @return mixed |
222
|
|
|
*/ |
223
|
4 |
|
protected function prepareSelection(NetteDatabaseSelection $selection) |
224
|
|
|
{ |
225
|
4 |
|
$selectionClass = $this->structure->getSelectionClass($selection->getName()); |
226
|
4 |
|
return new $selectionClass($selection, $this->structure); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|