@@ -9,25 +9,25 @@ |
||
9 | 9 | */ |
10 | 10 | interface QueryFactory |
11 | 11 | { |
12 | - /** |
|
13 | - * Sets the ORDER BY directive executed in SQL. |
|
14 | - * |
|
15 | - * For instance: |
|
16 | - * |
|
17 | - * $queryFactory->sort('label ASC, status DESC'); |
|
18 | - * |
|
19 | - * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter. |
|
20 | - * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter: |
|
21 | - * |
|
22 | - * $queryFactory->sort(new UncheckedOrderBy('RAND()')) |
|
23 | - * |
|
24 | - * @param string|UncheckedOrderBy|null $orderBy |
|
25 | - */ |
|
26 | - public function sort($orderBy); |
|
12 | + /** |
|
13 | + * Sets the ORDER BY directive executed in SQL. |
|
14 | + * |
|
15 | + * For instance: |
|
16 | + * |
|
17 | + * $queryFactory->sort('label ASC, status DESC'); |
|
18 | + * |
|
19 | + * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter. |
|
20 | + * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter: |
|
21 | + * |
|
22 | + * $queryFactory->sort(new UncheckedOrderBy('RAND()')) |
|
23 | + * |
|
24 | + * @param string|UncheckedOrderBy|null $orderBy |
|
25 | + */ |
|
26 | + public function sort($orderBy); |
|
27 | 27 | |
28 | - public function getMagicSql() : string; |
|
28 | + public function getMagicSql() : string; |
|
29 | 29 | |
30 | - public function getMagicSqlCount() : string; |
|
30 | + public function getMagicSqlCount() : string; |
|
31 | 31 | |
32 | - public function getColumnDescriptors() : array; |
|
32 | + public function getColumnDescriptors() : array; |
|
33 | 33 | } |
@@ -32,285 +32,285 @@ |
||
32 | 32 | */ |
33 | 33 | class ResultIterator implements Result, \ArrayAccess, \JsonSerializable |
34 | 34 | { |
35 | - /** |
|
36 | - * @var Statement |
|
37 | - */ |
|
38 | - protected $statement; |
|
39 | - |
|
40 | - private $objectStorage; |
|
41 | - private $className; |
|
42 | - |
|
43 | - private $tdbmService; |
|
44 | - private $parameters; |
|
45 | - private $magicQuery; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var QueryFactory |
|
49 | - */ |
|
50 | - private $queryFactory; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var InnerResultIterator |
|
54 | - */ |
|
55 | - private $innerResultIterator; |
|
56 | - |
|
57 | - private $databasePlatform; |
|
58 | - |
|
59 | - private $totalCount; |
|
60 | - |
|
61 | - private $mode; |
|
62 | - |
|
63 | - private $logger; |
|
64 | - |
|
65 | - public function __construct(QueryFactory $queryFactory, array $parameters, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode, LoggerInterface $logger) |
|
66 | - { |
|
67 | - if ($mode !== null && $mode !== TDBMService::MODE_CURSOR && $mode !== TDBMService::MODE_ARRAY) { |
|
68 | - throw new TDBMException("Unknown fetch mode: '".$mode."'"); |
|
69 | - } |
|
70 | - |
|
71 | - $this->queryFactory = $queryFactory; |
|
72 | - $this->objectStorage = $objectStorage; |
|
73 | - $this->className = $className; |
|
74 | - $this->tdbmService = $tdbmService; |
|
75 | - $this->parameters = $parameters; |
|
76 | - $this->magicQuery = $magicQuery; |
|
77 | - $this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform(); |
|
78 | - $this->mode = $mode; |
|
79 | - $this->logger = $logger; |
|
80 | - } |
|
81 | - |
|
82 | - protected function executeCountQuery() |
|
83 | - { |
|
84 | - $sql = $this->magicQuery->build($this->queryFactory->getMagicSqlCount(), $this->parameters); |
|
85 | - $this->logger->debug('Running count query: '.$sql); |
|
86 | - $this->totalCount = $this->tdbmService->getConnection()->fetchColumn($sql, $this->parameters); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings). |
|
91 | - * |
|
92 | - * @return int |
|
93 | - */ |
|
94 | - public function count() |
|
95 | - { |
|
96 | - if ($this->totalCount === null) { |
|
97 | - $this->executeCountQuery(); |
|
98 | - } |
|
99 | - |
|
100 | - return $this->totalCount; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Casts the result set to a PHP array. |
|
105 | - * |
|
106 | - * @return array |
|
107 | - */ |
|
108 | - public function toArray() |
|
109 | - { |
|
110 | - return iterator_to_array($this->getIterator()); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Returns a new iterator mapping any call using the $callable function. |
|
115 | - * |
|
116 | - * @param callable $callable |
|
117 | - * |
|
118 | - * @return MapIterator |
|
119 | - */ |
|
120 | - public function map(callable $callable) |
|
121 | - { |
|
122 | - return new MapIterator($this->getIterator(), $callable); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Retrieve an external iterator. |
|
127 | - * |
|
128 | - * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
129 | - * |
|
130 | - * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or |
|
131 | - * <b>Traversable</b> |
|
132 | - * |
|
133 | - * @since 5.0.0 |
|
134 | - */ |
|
135 | - public function getIterator() |
|
136 | - { |
|
137 | - if ($this->innerResultIterator === null) { |
|
138 | - if ($this->mode === TDBMService::MODE_CURSOR) { |
|
139 | - $this->innerResultIterator = new InnerResultIterator($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger); |
|
140 | - } else { |
|
141 | - $this->innerResultIterator = new InnerResultArray($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - return $this->innerResultIterator; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @param int $offset |
|
150 | - * @param int $limit |
|
151 | - * |
|
152 | - * @return PageIterator |
|
153 | - */ |
|
154 | - public function take($offset, $limit) |
|
155 | - { |
|
156 | - return new PageIterator($this, $this->queryFactory->getMagicSql(), $this->parameters, $limit, $offset, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->mode, $this->logger); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Whether a offset exists. |
|
161 | - * |
|
162 | - * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
163 | - * |
|
164 | - * @param mixed $offset <p> |
|
165 | - * An offset to check for. |
|
166 | - * </p> |
|
167 | - * |
|
168 | - * @return bool true on success or false on failure. |
|
169 | - * </p> |
|
170 | - * <p> |
|
171 | - * The return value will be casted to boolean if non-boolean was returned |
|
172 | - * |
|
173 | - * @since 5.0.0 |
|
174 | - */ |
|
175 | - public function offsetExists($offset) |
|
176 | - { |
|
177 | - return $this->getIterator()->offsetExists($offset); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Offset to retrieve. |
|
182 | - * |
|
183 | - * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
184 | - * |
|
185 | - * @param mixed $offset <p> |
|
186 | - * The offset to retrieve. |
|
187 | - * </p> |
|
188 | - * |
|
189 | - * @return mixed Can return all value types |
|
190 | - * |
|
191 | - * @since 5.0.0 |
|
192 | - */ |
|
193 | - public function offsetGet($offset) |
|
194 | - { |
|
195 | - return $this->getIterator()->offsetGet($offset); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Offset to set. |
|
200 | - * |
|
201 | - * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
202 | - * |
|
203 | - * @param mixed $offset <p> |
|
204 | - * The offset to assign the value to. |
|
205 | - * </p> |
|
206 | - * @param mixed $value <p> |
|
207 | - * The value to set. |
|
208 | - * </p> |
|
209 | - * |
|
210 | - * @since 5.0.0 |
|
211 | - */ |
|
212 | - public function offsetSet($offset, $value) |
|
213 | - { |
|
214 | - return $this->getIterator()->offsetSet($offset, $value); |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Offset to unset. |
|
219 | - * |
|
220 | - * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
221 | - * |
|
222 | - * @param mixed $offset <p> |
|
223 | - * The offset to unset. |
|
224 | - * </p> |
|
225 | - * |
|
226 | - * @since 5.0.0 |
|
227 | - */ |
|
228 | - public function offsetUnset($offset) |
|
229 | - { |
|
230 | - return $this->getIterator()->offsetUnset($offset); |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Specify data which should be serialized to JSON. |
|
235 | - * |
|
236 | - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
237 | - * |
|
238 | - * @param bool $stopRecursion Parameter used internally by TDBM to |
|
239 | - * stop embedded objects from embedding |
|
240 | - * other objects |
|
241 | - * |
|
242 | - * @return mixed data which can be serialized by <b>json_encode</b>, |
|
243 | - * which is a value of any type other than a resource |
|
244 | - * |
|
245 | - * @since 5.4.0 |
|
246 | - */ |
|
247 | - public function jsonSerialize($stopRecursion = false) |
|
248 | - { |
|
249 | - return array_map(function (AbstractTDBMObject $item) use ($stopRecursion) { |
|
250 | - return $item->jsonSerialize($stopRecursion); |
|
251 | - }, $this->toArray()); |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * Returns only one value (the first) of the result set. |
|
256 | - * Returns null if no value exists. |
|
257 | - * |
|
258 | - * @return mixed|null |
|
259 | - */ |
|
260 | - public function first() |
|
261 | - { |
|
262 | - $page = $this->take(0, 1); |
|
263 | - foreach ($page as $bean) { |
|
264 | - return $bean; |
|
265 | - } |
|
266 | - |
|
267 | - return; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Sets the ORDER BY directive executed in SQL and returns a NEW ResultIterator. |
|
272 | - * |
|
273 | - * For instance: |
|
274 | - * |
|
275 | - * $resultSet = $resultSet->withOrder('label ASC, status DESC'); |
|
276 | - * |
|
277 | - * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter. |
|
278 | - * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter: |
|
279 | - * |
|
280 | - * $resultSet->withOrder(new UncheckedOrderBy('RAND()')) |
|
281 | - * |
|
282 | - * @param string|UncheckedOrderBy|null $orderBy |
|
283 | - * |
|
284 | - * @return ResultIterator |
|
285 | - */ |
|
286 | - public function withOrder($orderBy) : ResultIterator |
|
287 | - { |
|
288 | - $clone = clone $this; |
|
289 | - $clone->queryFactory = clone $this->queryFactory; |
|
290 | - $clone->queryFactory->sort($orderBy); |
|
291 | - $clone->innerResultIterator = null; |
|
292 | - |
|
293 | - return $clone; |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Sets new parameters for the SQL query and returns a NEW ResultIterator. |
|
298 | - * |
|
299 | - * For instance: |
|
300 | - * |
|
301 | - * $resultSet = $resultSet->withParameters('label ASC, status DESC'); |
|
302 | - * |
|
303 | - * @param string|UncheckedOrderBy|null $orderBy |
|
304 | - * |
|
305 | - * @return ResultIterator |
|
306 | - */ |
|
307 | - public function withParameters(array $parameters) : ResultIterator |
|
308 | - { |
|
309 | - $clone = clone $this; |
|
310 | - $clone->parameters = $parameters; |
|
311 | - $clone->innerResultIterator = null; |
|
312 | - $clone->totalCount = null; |
|
313 | - |
|
314 | - return $clone; |
|
315 | - } |
|
35 | + /** |
|
36 | + * @var Statement |
|
37 | + */ |
|
38 | + protected $statement; |
|
39 | + |
|
40 | + private $objectStorage; |
|
41 | + private $className; |
|
42 | + |
|
43 | + private $tdbmService; |
|
44 | + private $parameters; |
|
45 | + private $magicQuery; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var QueryFactory |
|
49 | + */ |
|
50 | + private $queryFactory; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var InnerResultIterator |
|
54 | + */ |
|
55 | + private $innerResultIterator; |
|
56 | + |
|
57 | + private $databasePlatform; |
|
58 | + |
|
59 | + private $totalCount; |
|
60 | + |
|
61 | + private $mode; |
|
62 | + |
|
63 | + private $logger; |
|
64 | + |
|
65 | + public function __construct(QueryFactory $queryFactory, array $parameters, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode, LoggerInterface $logger) |
|
66 | + { |
|
67 | + if ($mode !== null && $mode !== TDBMService::MODE_CURSOR && $mode !== TDBMService::MODE_ARRAY) { |
|
68 | + throw new TDBMException("Unknown fetch mode: '".$mode."'"); |
|
69 | + } |
|
70 | + |
|
71 | + $this->queryFactory = $queryFactory; |
|
72 | + $this->objectStorage = $objectStorage; |
|
73 | + $this->className = $className; |
|
74 | + $this->tdbmService = $tdbmService; |
|
75 | + $this->parameters = $parameters; |
|
76 | + $this->magicQuery = $magicQuery; |
|
77 | + $this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform(); |
|
78 | + $this->mode = $mode; |
|
79 | + $this->logger = $logger; |
|
80 | + } |
|
81 | + |
|
82 | + protected function executeCountQuery() |
|
83 | + { |
|
84 | + $sql = $this->magicQuery->build($this->queryFactory->getMagicSqlCount(), $this->parameters); |
|
85 | + $this->logger->debug('Running count query: '.$sql); |
|
86 | + $this->totalCount = $this->tdbmService->getConnection()->fetchColumn($sql, $this->parameters); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings). |
|
91 | + * |
|
92 | + * @return int |
|
93 | + */ |
|
94 | + public function count() |
|
95 | + { |
|
96 | + if ($this->totalCount === null) { |
|
97 | + $this->executeCountQuery(); |
|
98 | + } |
|
99 | + |
|
100 | + return $this->totalCount; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Casts the result set to a PHP array. |
|
105 | + * |
|
106 | + * @return array |
|
107 | + */ |
|
108 | + public function toArray() |
|
109 | + { |
|
110 | + return iterator_to_array($this->getIterator()); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Returns a new iterator mapping any call using the $callable function. |
|
115 | + * |
|
116 | + * @param callable $callable |
|
117 | + * |
|
118 | + * @return MapIterator |
|
119 | + */ |
|
120 | + public function map(callable $callable) |
|
121 | + { |
|
122 | + return new MapIterator($this->getIterator(), $callable); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Retrieve an external iterator. |
|
127 | + * |
|
128 | + * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
129 | + * |
|
130 | + * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or |
|
131 | + * <b>Traversable</b> |
|
132 | + * |
|
133 | + * @since 5.0.0 |
|
134 | + */ |
|
135 | + public function getIterator() |
|
136 | + { |
|
137 | + if ($this->innerResultIterator === null) { |
|
138 | + if ($this->mode === TDBMService::MODE_CURSOR) { |
|
139 | + $this->innerResultIterator = new InnerResultIterator($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger); |
|
140 | + } else { |
|
141 | + $this->innerResultIterator = new InnerResultArray($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + return $this->innerResultIterator; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @param int $offset |
|
150 | + * @param int $limit |
|
151 | + * |
|
152 | + * @return PageIterator |
|
153 | + */ |
|
154 | + public function take($offset, $limit) |
|
155 | + { |
|
156 | + return new PageIterator($this, $this->queryFactory->getMagicSql(), $this->parameters, $limit, $offset, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->mode, $this->logger); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Whether a offset exists. |
|
161 | + * |
|
162 | + * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
163 | + * |
|
164 | + * @param mixed $offset <p> |
|
165 | + * An offset to check for. |
|
166 | + * </p> |
|
167 | + * |
|
168 | + * @return bool true on success or false on failure. |
|
169 | + * </p> |
|
170 | + * <p> |
|
171 | + * The return value will be casted to boolean if non-boolean was returned |
|
172 | + * |
|
173 | + * @since 5.0.0 |
|
174 | + */ |
|
175 | + public function offsetExists($offset) |
|
176 | + { |
|
177 | + return $this->getIterator()->offsetExists($offset); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Offset to retrieve. |
|
182 | + * |
|
183 | + * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
184 | + * |
|
185 | + * @param mixed $offset <p> |
|
186 | + * The offset to retrieve. |
|
187 | + * </p> |
|
188 | + * |
|
189 | + * @return mixed Can return all value types |
|
190 | + * |
|
191 | + * @since 5.0.0 |
|
192 | + */ |
|
193 | + public function offsetGet($offset) |
|
194 | + { |
|
195 | + return $this->getIterator()->offsetGet($offset); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Offset to set. |
|
200 | + * |
|
201 | + * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
202 | + * |
|
203 | + * @param mixed $offset <p> |
|
204 | + * The offset to assign the value to. |
|
205 | + * </p> |
|
206 | + * @param mixed $value <p> |
|
207 | + * The value to set. |
|
208 | + * </p> |
|
209 | + * |
|
210 | + * @since 5.0.0 |
|
211 | + */ |
|
212 | + public function offsetSet($offset, $value) |
|
213 | + { |
|
214 | + return $this->getIterator()->offsetSet($offset, $value); |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Offset to unset. |
|
219 | + * |
|
220 | + * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
221 | + * |
|
222 | + * @param mixed $offset <p> |
|
223 | + * The offset to unset. |
|
224 | + * </p> |
|
225 | + * |
|
226 | + * @since 5.0.0 |
|
227 | + */ |
|
228 | + public function offsetUnset($offset) |
|
229 | + { |
|
230 | + return $this->getIterator()->offsetUnset($offset); |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Specify data which should be serialized to JSON. |
|
235 | + * |
|
236 | + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
237 | + * |
|
238 | + * @param bool $stopRecursion Parameter used internally by TDBM to |
|
239 | + * stop embedded objects from embedding |
|
240 | + * other objects |
|
241 | + * |
|
242 | + * @return mixed data which can be serialized by <b>json_encode</b>, |
|
243 | + * which is a value of any type other than a resource |
|
244 | + * |
|
245 | + * @since 5.4.0 |
|
246 | + */ |
|
247 | + public function jsonSerialize($stopRecursion = false) |
|
248 | + { |
|
249 | + return array_map(function (AbstractTDBMObject $item) use ($stopRecursion) { |
|
250 | + return $item->jsonSerialize($stopRecursion); |
|
251 | + }, $this->toArray()); |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * Returns only one value (the first) of the result set. |
|
256 | + * Returns null if no value exists. |
|
257 | + * |
|
258 | + * @return mixed|null |
|
259 | + */ |
|
260 | + public function first() |
|
261 | + { |
|
262 | + $page = $this->take(0, 1); |
|
263 | + foreach ($page as $bean) { |
|
264 | + return $bean; |
|
265 | + } |
|
266 | + |
|
267 | + return; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Sets the ORDER BY directive executed in SQL and returns a NEW ResultIterator. |
|
272 | + * |
|
273 | + * For instance: |
|
274 | + * |
|
275 | + * $resultSet = $resultSet->withOrder('label ASC, status DESC'); |
|
276 | + * |
|
277 | + * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter. |
|
278 | + * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter: |
|
279 | + * |
|
280 | + * $resultSet->withOrder(new UncheckedOrderBy('RAND()')) |
|
281 | + * |
|
282 | + * @param string|UncheckedOrderBy|null $orderBy |
|
283 | + * |
|
284 | + * @return ResultIterator |
|
285 | + */ |
|
286 | + public function withOrder($orderBy) : ResultIterator |
|
287 | + { |
|
288 | + $clone = clone $this; |
|
289 | + $clone->queryFactory = clone $this->queryFactory; |
|
290 | + $clone->queryFactory->sort($orderBy); |
|
291 | + $clone->innerResultIterator = null; |
|
292 | + |
|
293 | + return $clone; |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Sets new parameters for the SQL query and returns a NEW ResultIterator. |
|
298 | + * |
|
299 | + * For instance: |
|
300 | + * |
|
301 | + * $resultSet = $resultSet->withParameters('label ASC, status DESC'); |
|
302 | + * |
|
303 | + * @param string|UncheckedOrderBy|null $orderBy |
|
304 | + * |
|
305 | + * @return ResultIterator |
|
306 | + */ |
|
307 | + public function withParameters(array $parameters) : ResultIterator |
|
308 | + { |
|
309 | + $clone = clone $this; |
|
310 | + $clone->parameters = $parameters; |
|
311 | + $clone->innerResultIterator = null; |
|
312 | + $clone->totalCount = null; |
|
313 | + |
|
314 | + return $clone; |
|
315 | + } |
|
316 | 316 | } |
@@ -4,36 +4,36 @@ |
||
4 | 4 | |
5 | 5 | interface MethodDescriptorInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * Returns the name of the method to be generated. |
|
9 | - * |
|
10 | - * @return string |
|
11 | - */ |
|
12 | - public function getName() : string; |
|
7 | + /** |
|
8 | + * Returns the name of the method to be generated. |
|
9 | + * |
|
10 | + * @return string |
|
11 | + */ |
|
12 | + public function getName() : string; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Requests the use of an alternative name for this method. |
|
16 | - */ |
|
17 | - public function useAlternativeName(); |
|
14 | + /** |
|
15 | + * Requests the use of an alternative name for this method. |
|
16 | + */ |
|
17 | + public function useAlternativeName(); |
|
18 | 18 | |
19 | - /** |
|
20 | - * Returns the code of the method. |
|
21 | - * |
|
22 | - * @return string |
|
23 | - */ |
|
24 | - public function getCode() : string; |
|
19 | + /** |
|
20 | + * Returns the code of the method. |
|
21 | + * |
|
22 | + * @return string |
|
23 | + */ |
|
24 | + public function getCode() : string; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Returns an array of classes that needs a "use" for this method. |
|
28 | - * |
|
29 | - * @return string[] |
|
30 | - */ |
|
31 | - public function getUsedClasses() : array; |
|
26 | + /** |
|
27 | + * Returns an array of classes that needs a "use" for this method. |
|
28 | + * |
|
29 | + * @return string[] |
|
30 | + */ |
|
31 | + public function getUsedClasses() : array; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Returns the code to past in jsonSerialize. |
|
35 | - * |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - public function getJsonSerializeCode() : string; |
|
33 | + /** |
|
34 | + * Returns the code to past in jsonSerialize. |
|
35 | + * |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + public function getJsonSerializeCode() : string; |
|
39 | 39 | } |
@@ -14,274 +14,274 @@ |
||
14 | 14 | */ |
15 | 15 | class AlterableResultIterator implements Result, \ArrayAccess, \JsonSerializable |
16 | 16 | { |
17 | - /** |
|
18 | - * @var \Iterator|null |
|
19 | - */ |
|
20 | - private $resultIterator; |
|
17 | + /** |
|
18 | + * @var \Iterator|null |
|
19 | + */ |
|
20 | + private $resultIterator; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Key: the object to alter in the result set. |
|
24 | - * Value: "add" => the object will be added to the resultset (if it is not found in it) |
|
25 | - * "delete" => the object will be removed from the resultset (if found). |
|
26 | - * |
|
27 | - * @var \SplObjectStorage |
|
28 | - */ |
|
29 | - private $alterations; |
|
22 | + /** |
|
23 | + * Key: the object to alter in the result set. |
|
24 | + * Value: "add" => the object will be added to the resultset (if it is not found in it) |
|
25 | + * "delete" => the object will be removed from the resultset (if found). |
|
26 | + * |
|
27 | + * @var \SplObjectStorage |
|
28 | + */ |
|
29 | + private $alterations; |
|
30 | 30 | |
31 | - /** |
|
32 | - * The result array from the result set. |
|
33 | - * |
|
34 | - * @var array|null |
|
35 | - */ |
|
36 | - private $resultArray; |
|
31 | + /** |
|
32 | + * The result array from the result set. |
|
33 | + * |
|
34 | + * @var array|null |
|
35 | + */ |
|
36 | + private $resultArray; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @param \Iterator|null $resultIterator |
|
40 | - */ |
|
41 | - public function __construct(\Iterator $resultIterator = null) |
|
42 | - { |
|
43 | - $this->resultIterator = $resultIterator; |
|
44 | - $this->alterations = new \SplObjectStorage(); |
|
45 | - } |
|
38 | + /** |
|
39 | + * @param \Iterator|null $resultIterator |
|
40 | + */ |
|
41 | + public function __construct(\Iterator $resultIterator = null) |
|
42 | + { |
|
43 | + $this->resultIterator = $resultIterator; |
|
44 | + $this->alterations = new \SplObjectStorage(); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Sets a new iterator as the base iterator to be altered. |
|
49 | - * |
|
50 | - * @param \Iterator $resultIterator |
|
51 | - */ |
|
52 | - public function setResultIterator(\Iterator $resultIterator) |
|
53 | - { |
|
54 | - $this->resultIterator = $resultIterator; |
|
55 | - $this->resultArray = null; |
|
56 | - } |
|
47 | + /** |
|
48 | + * Sets a new iterator as the base iterator to be altered. |
|
49 | + * |
|
50 | + * @param \Iterator $resultIterator |
|
51 | + */ |
|
52 | + public function setResultIterator(\Iterator $resultIterator) |
|
53 | + { |
|
54 | + $this->resultIterator = $resultIterator; |
|
55 | + $this->resultArray = null; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * Returns the non altered result iterator (or null if none exist). |
|
60 | - * |
|
61 | - * @return \Iterator|null |
|
62 | - */ |
|
63 | - public function getUnderlyingResultIterator() |
|
64 | - { |
|
65 | - return $this->resultIterator; |
|
66 | - } |
|
58 | + /** |
|
59 | + * Returns the non altered result iterator (or null if none exist). |
|
60 | + * |
|
61 | + * @return \Iterator|null |
|
62 | + */ |
|
63 | + public function getUnderlyingResultIterator() |
|
64 | + { |
|
65 | + return $this->resultIterator; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * Adds an additional object to the result set (if not already available). |
|
70 | - * |
|
71 | - * @param $object |
|
72 | - */ |
|
73 | - public function add($object) |
|
74 | - { |
|
75 | - $this->alterations->attach($object, 'add'); |
|
68 | + /** |
|
69 | + * Adds an additional object to the result set (if not already available). |
|
70 | + * |
|
71 | + * @param $object |
|
72 | + */ |
|
73 | + public function add($object) |
|
74 | + { |
|
75 | + $this->alterations->attach($object, 'add'); |
|
76 | 76 | |
77 | - if ($this->resultArray !== null) { |
|
78 | - $foundKey = array_search($object, $this->resultArray, true); |
|
79 | - if ($foundKey === false) { |
|
80 | - $this->resultArray[] = $object; |
|
81 | - } |
|
82 | - } |
|
83 | - } |
|
77 | + if ($this->resultArray !== null) { |
|
78 | + $foundKey = array_search($object, $this->resultArray, true); |
|
79 | + if ($foundKey === false) { |
|
80 | + $this->resultArray[] = $object; |
|
81 | + } |
|
82 | + } |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Removes an object from the result set. |
|
87 | - * |
|
88 | - * @param $object |
|
89 | - */ |
|
90 | - public function remove($object) |
|
91 | - { |
|
92 | - $this->alterations->attach($object, 'delete'); |
|
85 | + /** |
|
86 | + * Removes an object from the result set. |
|
87 | + * |
|
88 | + * @param $object |
|
89 | + */ |
|
90 | + public function remove($object) |
|
91 | + { |
|
92 | + $this->alterations->attach($object, 'delete'); |
|
93 | 93 | |
94 | - if ($this->resultArray !== null) { |
|
95 | - $foundKey = array_search($object, $this->resultArray, true); |
|
96 | - if ($foundKey !== false) { |
|
97 | - unset($this->resultArray[$foundKey]); |
|
98 | - } |
|
99 | - } |
|
100 | - } |
|
94 | + if ($this->resultArray !== null) { |
|
95 | + $foundKey = array_search($object, $this->resultArray, true); |
|
96 | + if ($foundKey !== false) { |
|
97 | + unset($this->resultArray[$foundKey]); |
|
98 | + } |
|
99 | + } |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Casts the result set to a PHP array. |
|
104 | - * |
|
105 | - * @return array |
|
106 | - */ |
|
107 | - public function toArray() |
|
108 | - { |
|
109 | - if ($this->resultArray === null) { |
|
110 | - if ($this->resultIterator !== null) { |
|
111 | - $this->resultArray = iterator_to_array($this->resultIterator); |
|
112 | - } else { |
|
113 | - $this->resultArray = []; |
|
114 | - } |
|
102 | + /** |
|
103 | + * Casts the result set to a PHP array. |
|
104 | + * |
|
105 | + * @return array |
|
106 | + */ |
|
107 | + public function toArray() |
|
108 | + { |
|
109 | + if ($this->resultArray === null) { |
|
110 | + if ($this->resultIterator !== null) { |
|
111 | + $this->resultArray = iterator_to_array($this->resultIterator); |
|
112 | + } else { |
|
113 | + $this->resultArray = []; |
|
114 | + } |
|
115 | 115 | |
116 | - foreach ($this->alterations as $obj) { |
|
117 | - $action = $this->alterations->getInfo(); // return, if exists, associated with cur. obj. data; else NULL |
|
116 | + foreach ($this->alterations as $obj) { |
|
117 | + $action = $this->alterations->getInfo(); // return, if exists, associated with cur. obj. data; else NULL |
|
118 | 118 | |
119 | - $foundKey = array_search($obj, $this->resultArray, true); |
|
119 | + $foundKey = array_search($obj, $this->resultArray, true); |
|
120 | 120 | |
121 | - if ($action === 'add' && $foundKey === false) { |
|
122 | - $this->resultArray[] = $obj; |
|
123 | - } elseif ($action === 'delete' && $foundKey !== false) { |
|
124 | - unset($this->resultArray[$foundKey]); |
|
125 | - } |
|
126 | - } |
|
127 | - } |
|
121 | + if ($action === 'add' && $foundKey === false) { |
|
122 | + $this->resultArray[] = $obj; |
|
123 | + } elseif ($action === 'delete' && $foundKey !== false) { |
|
124 | + unset($this->resultArray[$foundKey]); |
|
125 | + } |
|
126 | + } |
|
127 | + } |
|
128 | 128 | |
129 | - return array_values($this->resultArray); |
|
130 | - } |
|
129 | + return array_values($this->resultArray); |
|
130 | + } |
|
131 | 131 | |
132 | - /** |
|
133 | - * Whether a offset exists. |
|
134 | - * |
|
135 | - * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
136 | - * |
|
137 | - * @param mixed $offset <p> |
|
138 | - * An offset to check for. |
|
139 | - * </p> |
|
140 | - * |
|
141 | - * @return bool true on success or false on failure. |
|
142 | - * </p> |
|
143 | - * <p> |
|
144 | - * The return value will be casted to boolean if non-boolean was returned |
|
145 | - * |
|
146 | - * @since 5.0.0 |
|
147 | - */ |
|
148 | - public function offsetExists($offset) |
|
149 | - { |
|
150 | - return isset($this->toArray()[$offset]); |
|
151 | - } |
|
132 | + /** |
|
133 | + * Whether a offset exists. |
|
134 | + * |
|
135 | + * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
136 | + * |
|
137 | + * @param mixed $offset <p> |
|
138 | + * An offset to check for. |
|
139 | + * </p> |
|
140 | + * |
|
141 | + * @return bool true on success or false on failure. |
|
142 | + * </p> |
|
143 | + * <p> |
|
144 | + * The return value will be casted to boolean if non-boolean was returned |
|
145 | + * |
|
146 | + * @since 5.0.0 |
|
147 | + */ |
|
148 | + public function offsetExists($offset) |
|
149 | + { |
|
150 | + return isset($this->toArray()[$offset]); |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * Offset to retrieve. |
|
155 | - * |
|
156 | - * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
157 | - * |
|
158 | - * @param mixed $offset <p> |
|
159 | - * The offset to retrieve. |
|
160 | - * </p> |
|
161 | - * |
|
162 | - * @return mixed Can return all value types |
|
163 | - * |
|
164 | - * @since 5.0.0 |
|
165 | - */ |
|
166 | - public function offsetGet($offset) |
|
167 | - { |
|
168 | - return $this->toArray()[$offset]; |
|
169 | - } |
|
153 | + /** |
|
154 | + * Offset to retrieve. |
|
155 | + * |
|
156 | + * @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
157 | + * |
|
158 | + * @param mixed $offset <p> |
|
159 | + * The offset to retrieve. |
|
160 | + * </p> |
|
161 | + * |
|
162 | + * @return mixed Can return all value types |
|
163 | + * |
|
164 | + * @since 5.0.0 |
|
165 | + */ |
|
166 | + public function offsetGet($offset) |
|
167 | + { |
|
168 | + return $this->toArray()[$offset]; |
|
169 | + } |
|
170 | 170 | |
171 | - /** |
|
172 | - * Offset to set. |
|
173 | - * |
|
174 | - * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
175 | - * |
|
176 | - * @param mixed $offset <p> |
|
177 | - * The offset to assign the value to. |
|
178 | - * </p> |
|
179 | - * @param mixed $value <p> |
|
180 | - * The value to set. |
|
181 | - * </p> |
|
182 | - * |
|
183 | - * @since 5.0.0 |
|
184 | - */ |
|
185 | - public function offsetSet($offset, $value) |
|
186 | - { |
|
187 | - throw new TDBMInvalidOperationException('You can set values in a TDBM result set, even in an alterable one. Use the add method instead.'); |
|
188 | - } |
|
171 | + /** |
|
172 | + * Offset to set. |
|
173 | + * |
|
174 | + * @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
175 | + * |
|
176 | + * @param mixed $offset <p> |
|
177 | + * The offset to assign the value to. |
|
178 | + * </p> |
|
179 | + * @param mixed $value <p> |
|
180 | + * The value to set. |
|
181 | + * </p> |
|
182 | + * |
|
183 | + * @since 5.0.0 |
|
184 | + */ |
|
185 | + public function offsetSet($offset, $value) |
|
186 | + { |
|
187 | + throw new TDBMInvalidOperationException('You can set values in a TDBM result set, even in an alterable one. Use the add method instead.'); |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * Offset to unset. |
|
192 | - * |
|
193 | - * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
194 | - * |
|
195 | - * @param mixed $offset <p> |
|
196 | - * The offset to unset. |
|
197 | - * </p> |
|
198 | - * |
|
199 | - * @since 5.0.0 |
|
200 | - */ |
|
201 | - public function offsetUnset($offset) |
|
202 | - { |
|
203 | - throw new TDBMInvalidOperationException('You can unset values in a TDBM result set, even in an alterable one. Use the delete method instead.'); |
|
204 | - } |
|
190 | + /** |
|
191 | + * Offset to unset. |
|
192 | + * |
|
193 | + * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
194 | + * |
|
195 | + * @param mixed $offset <p> |
|
196 | + * The offset to unset. |
|
197 | + * </p> |
|
198 | + * |
|
199 | + * @since 5.0.0 |
|
200 | + */ |
|
201 | + public function offsetUnset($offset) |
|
202 | + { |
|
203 | + throw new TDBMInvalidOperationException('You can unset values in a TDBM result set, even in an alterable one. Use the delete method instead.'); |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * @param int $offset |
|
208 | - * |
|
209 | - * @return \Porpaginas\Page |
|
210 | - */ |
|
211 | - public function take($offset, $limit) |
|
212 | - { |
|
213 | - // TODO: replace this with a class implementing the map method. |
|
214 | - return new ArrayPage(array_slice($this->toArray(), $offset, $limit), $offset, $limit, count($this->toArray())); |
|
215 | - } |
|
206 | + /** |
|
207 | + * @param int $offset |
|
208 | + * |
|
209 | + * @return \Porpaginas\Page |
|
210 | + */ |
|
211 | + public function take($offset, $limit) |
|
212 | + { |
|
213 | + // TODO: replace this with a class implementing the map method. |
|
214 | + return new ArrayPage(array_slice($this->toArray(), $offset, $limit), $offset, $limit, count($this->toArray())); |
|
215 | + } |
|
216 | 216 | |
217 | - /** |
|
218 | - * Return the number of all results in the paginatable. |
|
219 | - * |
|
220 | - * @return int |
|
221 | - */ |
|
222 | - public function count() |
|
223 | - { |
|
224 | - return count($this->toArray()); |
|
225 | - } |
|
217 | + /** |
|
218 | + * Return the number of all results in the paginatable. |
|
219 | + * |
|
220 | + * @return int |
|
221 | + */ |
|
222 | + public function count() |
|
223 | + { |
|
224 | + return count($this->toArray()); |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * Return an iterator over all results of the paginatable. |
|
229 | - * |
|
230 | - * @return Iterator |
|
231 | - */ |
|
232 | - public function getIterator() |
|
233 | - { |
|
234 | - if ($this->alterations->count() === 0) { |
|
235 | - if ($this->resultIterator !== null) { |
|
236 | - return $this->resultIterator; |
|
237 | - } else { |
|
238 | - return new \ArrayIterator([]); |
|
239 | - } |
|
240 | - } else { |
|
241 | - return new \ArrayIterator($this->toArray()); |
|
242 | - } |
|
243 | - } |
|
227 | + /** |
|
228 | + * Return an iterator over all results of the paginatable. |
|
229 | + * |
|
230 | + * @return Iterator |
|
231 | + */ |
|
232 | + public function getIterator() |
|
233 | + { |
|
234 | + if ($this->alterations->count() === 0) { |
|
235 | + if ($this->resultIterator !== null) { |
|
236 | + return $this->resultIterator; |
|
237 | + } else { |
|
238 | + return new \ArrayIterator([]); |
|
239 | + } |
|
240 | + } else { |
|
241 | + return new \ArrayIterator($this->toArray()); |
|
242 | + } |
|
243 | + } |
|
244 | 244 | |
245 | - /** |
|
246 | - * Specify data which should be serialized to JSON. |
|
247 | - * |
|
248 | - * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
249 | - * |
|
250 | - * @return mixed data which can be serialized by <b>json_encode</b>, |
|
251 | - * which is a value of any type other than a resource |
|
252 | - * |
|
253 | - * @since 5.4.0 |
|
254 | - */ |
|
255 | - public function jsonSerialize() |
|
256 | - { |
|
257 | - return $this->toArray(); |
|
258 | - } |
|
245 | + /** |
|
246 | + * Specify data which should be serialized to JSON. |
|
247 | + * |
|
248 | + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
249 | + * |
|
250 | + * @return mixed data which can be serialized by <b>json_encode</b>, |
|
251 | + * which is a value of any type other than a resource |
|
252 | + * |
|
253 | + * @since 5.4.0 |
|
254 | + */ |
|
255 | + public function jsonSerialize() |
|
256 | + { |
|
257 | + return $this->toArray(); |
|
258 | + } |
|
259 | 259 | |
260 | - /** |
|
261 | - * Returns only one value (the first) of the result set. |
|
262 | - * Returns null if no value exists. |
|
263 | - * |
|
264 | - * @return mixed|null |
|
265 | - */ |
|
266 | - public function first() |
|
267 | - { |
|
268 | - $page = $this->take(0, 1); |
|
269 | - foreach ($page as $bean) { |
|
270 | - return $bean; |
|
271 | - } |
|
260 | + /** |
|
261 | + * Returns only one value (the first) of the result set. |
|
262 | + * Returns null if no value exists. |
|
263 | + * |
|
264 | + * @return mixed|null |
|
265 | + */ |
|
266 | + public function first() |
|
267 | + { |
|
268 | + $page = $this->take(0, 1); |
|
269 | + foreach ($page as $bean) { |
|
270 | + return $bean; |
|
271 | + } |
|
272 | 272 | |
273 | - return; |
|
274 | - } |
|
273 | + return; |
|
274 | + } |
|
275 | 275 | |
276 | - /** |
|
277 | - * Returns a new iterator mapping any call using the $callable function. |
|
278 | - * |
|
279 | - * @param callable $callable |
|
280 | - * |
|
281 | - * @return MapIterator |
|
282 | - */ |
|
283 | - public function map(callable $callable) |
|
284 | - { |
|
285 | - return new MapIterator($this->getIterator(), $callable); |
|
286 | - } |
|
276 | + /** |
|
277 | + * Returns a new iterator mapping any call using the $callable function. |
|
278 | + * |
|
279 | + * @param callable $callable |
|
280 | + * |
|
281 | + * @return MapIterator |
|
282 | + */ |
|
283 | + public function map(callable $callable) |
|
284 | + { |
|
285 | + return new MapIterator($this->getIterator(), $callable); |
|
286 | + } |
|
287 | 287 | } |
@@ -7,23 +7,23 @@ |
||
7 | 7 | */ |
8 | 8 | class TDBMInheritanceException extends TDBMException |
9 | 9 | { |
10 | - public static function create(array $tables) : TDBMInheritanceException |
|
11 | - { |
|
12 | - return new self(sprintf('The tables (%s) cannot be linked by an inheritance relationship. Does your data set contains multiple children for one parent row? (multiple inheritance is not supported by TDBM)', implode(', ', $tables))); |
|
13 | - } |
|
10 | + public static function create(array $tables) : TDBMInheritanceException |
|
11 | + { |
|
12 | + return new self(sprintf('The tables (%s) cannot be linked by an inheritance relationship. Does your data set contains multiple children for one parent row? (multiple inheritance is not supported by TDBM)', implode(', ', $tables))); |
|
13 | + } |
|
14 | 14 | |
15 | - public static function extendException(TDBMInheritanceException $e, TDBMService $tdbmService, array $beanData) : TDBMInheritanceException |
|
16 | - { |
|
17 | - $pks = []; |
|
18 | - foreach ($beanData as $table => $row) { |
|
19 | - $primaryKeyColumns = $tdbmService->getPrimaryKeyColumns($table); |
|
20 | - foreach ($primaryKeyColumns as $columnName) { |
|
21 | - if ($row[$columnName] !== null) { |
|
22 | - $pks[] = $table.'.'.$columnName.' => '.var_export($row[$columnName], true); |
|
23 | - } |
|
24 | - } |
|
25 | - } |
|
15 | + public static function extendException(TDBMInheritanceException $e, TDBMService $tdbmService, array $beanData) : TDBMInheritanceException |
|
16 | + { |
|
17 | + $pks = []; |
|
18 | + foreach ($beanData as $table => $row) { |
|
19 | + $primaryKeyColumns = $tdbmService->getPrimaryKeyColumns($table); |
|
20 | + foreach ($primaryKeyColumns as $columnName) { |
|
21 | + if ($row[$columnName] !== null) { |
|
22 | + $pks[] = $table.'.'.$columnName.' => '.var_export($row[$columnName], true); |
|
23 | + } |
|
24 | + } |
|
25 | + } |
|
26 | 26 | |
27 | - throw new self($e->getMessage().' (row in error: '.implode(', ', $pks).')', 0, $e); |
|
28 | - } |
|
27 | + throw new self($e->getMessage().' (row in error: '.implode(', ', $pks).')', 0, $e); |
|
28 | + } |
|
29 | 29 | } |
@@ -4,13 +4,13 @@ |
||
4 | 4 | |
5 | 5 | class TDBMCyclicReferenceException extends TDBMException |
6 | 6 | { |
7 | - public static function createCyclicReference(string $tableName, AbstractTDBMObject $object) : TDBMCyclicReferenceException |
|
8 | - { |
|
9 | - return new self(sprintf("You are trying a grape of objects that reference each other. Unable to save object '%s' in table '%s'. It is already in the process of being saved.", get_class($object), $tableName)); |
|
10 | - } |
|
7 | + public static function createCyclicReference(string $tableName, AbstractTDBMObject $object) : TDBMCyclicReferenceException |
|
8 | + { |
|
9 | + return new self(sprintf("You are trying a grape of objects that reference each other. Unable to save object '%s' in table '%s'. It is already in the process of being saved.", get_class($object), $tableName)); |
|
10 | + } |
|
11 | 11 | |
12 | - public static function extendCyclicReference(TDBMCyclicReferenceException $e, string $tableName, AbstractTDBMObject $object, string $fkName) : TDBMCyclicReferenceException |
|
13 | - { |
|
14 | - return new self($e->getMessage().sprintf(" This object is referenced by an object of type '%s' (table '%s') via foreign key '%s'", get_class($object), $tableName, $fkName), $e->getCode(), $e); |
|
15 | - } |
|
12 | + public static function extendCyclicReference(TDBMCyclicReferenceException $e, string $tableName, AbstractTDBMObject $object, string $fkName) : TDBMCyclicReferenceException |
|
13 | + { |
|
14 | + return new self($e->getMessage().sprintf(" This object is referenced by an object of type '%s' (table '%s') via foreign key '%s'", get_class($object), $tableName, $fkName), $e->getCode(), $e); |
|
15 | + } |
|
16 | 16 | } |
@@ -4,8 +4,8 @@ |
||
4 | 4 | |
5 | 5 | class TDBMMissingReferenceException extends TDBMException |
6 | 6 | { |
7 | - public static function referenceDeleted(string $tableName, AbstractTDBMObject $reference) : TDBMMissingReferenceException |
|
8 | - { |
|
9 | - return new self(sprintf("Unable to save object in table '%s'. Your object references an object of type '%s' that is deleted.", $tableName, get_class($reference))); |
|
10 | - } |
|
7 | + public static function referenceDeleted(string $tableName, AbstractTDBMObject $reference) : TDBMMissingReferenceException |
|
8 | + { |
|
9 | + return new self(sprintf("Unable to save object in table '%s'. Your object references an object of type '%s' that is deleted.", $tableName, get_class($reference))); |
|
10 | + } |
|
11 | 11 | } |
@@ -27,170 +27,170 @@ discard block |
||
27 | 27 | */ |
28 | 28 | class DbRow |
29 | 29 | { |
30 | - /** |
|
31 | - * The service this object is bound to. |
|
32 | - * |
|
33 | - * @var TDBMService |
|
34 | - */ |
|
35 | - protected $tdbmService; |
|
36 | - |
|
37 | - /** |
|
38 | - * The object containing this db row. |
|
39 | - * |
|
40 | - * @var AbstractTDBMObject |
|
41 | - */ |
|
42 | - private $object; |
|
43 | - |
|
44 | - /** |
|
45 | - * The name of the table the object if issued from. |
|
46 | - * |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - private $dbTableName; |
|
50 | - |
|
51 | - /** |
|
52 | - * The array of columns returned from database. |
|
53 | - * |
|
54 | - * @var array |
|
55 | - */ |
|
56 | - private $dbRow = array(); |
|
57 | - |
|
58 | - /** |
|
59 | - * @var AbstractTDBMObject[] |
|
60 | - */ |
|
61 | - private $references = array(); |
|
62 | - |
|
63 | - /** |
|
64 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
65 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
66 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
67 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
68 | - * |
|
69 | - * @var string |
|
70 | - */ |
|
71 | - private $status; |
|
72 | - |
|
73 | - /** |
|
74 | - * The values of the primary key. |
|
75 | - * This is set when the object is in "loaded" state. |
|
76 | - * |
|
77 | - * @var array An array of column => value |
|
78 | - */ |
|
79 | - private $primaryKeys; |
|
80 | - |
|
81 | - /** |
|
82 | - * You should never call the constructor directly. Instead, you should use the |
|
83 | - * TDBMService class that will create TDBMObjects for you. |
|
84 | - * |
|
85 | - * Used with id!=false when we want to retrieve an existing object |
|
86 | - * and id==false if we want a new object |
|
87 | - * |
|
88 | - * @param AbstractTDBMObject $object The object containing this db row |
|
89 | - * @param string $table_name |
|
90 | - * @param array $primaryKeys |
|
91 | - * @param TDBMService $tdbmService |
|
92 | - * |
|
93 | - * @throws TDBMException |
|
94 | - * @throws TDBMInvalidOperationException |
|
95 | - */ |
|
96 | - public function __construct(AbstractTDBMObject $object, $table_name, array $primaryKeys = array(), TDBMService $tdbmService = null, array $dbRow = array()) |
|
97 | - { |
|
98 | - $this->object = $object; |
|
99 | - $this->dbTableName = $table_name; |
|
100 | - |
|
101 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
102 | - |
|
103 | - if ($tdbmService === null) { |
|
104 | - if (!empty($primaryKeys)) { |
|
105 | - throw new TDBMException('You cannot pass an id to the DbRow constructor without passing also a TDBMService.'); |
|
106 | - } |
|
107 | - } else { |
|
108 | - $this->tdbmService = $tdbmService; |
|
109 | - |
|
110 | - if (!empty($primaryKeys)) { |
|
111 | - $this->_setPrimaryKeys($primaryKeys); |
|
112 | - if (!empty($dbRow)) { |
|
113 | - $this->dbRow = $dbRow; |
|
114 | - $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
115 | - } else { |
|
116 | - $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
117 | - } |
|
118 | - $tdbmService->_addToCache($this); |
|
119 | - } else { |
|
120 | - $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
121 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
122 | - } |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - public function _attach(TDBMService $tdbmService) |
|
127 | - { |
|
128 | - if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
129 | - throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
130 | - } |
|
131 | - $this->tdbmService = $tdbmService; |
|
132 | - $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
133 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Sets the state of the TDBM Object |
|
138 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
139 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
140 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
141 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
142 | - * |
|
143 | - * @param string $state |
|
144 | - */ |
|
145 | - public function _setStatus($state) |
|
146 | - { |
|
147 | - $this->status = $state; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * This is an internal method. You should not call this method yourself. The TDBM library will do it for you. |
|
152 | - * If the object is in state 'not loaded', this method performs a query in database to load the object. |
|
153 | - * |
|
154 | - * A TDBMException is thrown is no object can be retrieved (for instance, if the primary key specified |
|
155 | - * cannot be found). |
|
156 | - */ |
|
157 | - public function _dbLoadIfNotLoaded() |
|
158 | - { |
|
159 | - if ($this->status == TDBMObjectStateEnum::STATE_NOT_LOADED) { |
|
160 | - $connection = $this->tdbmService->getConnection(); |
|
161 | - |
|
162 | - /// buildFilterFromFilterBag($filter_bag) |
|
163 | - list($sql_where, $parameters) = $this->tdbmService->buildFilterFromFilterBag($this->primaryKeys); |
|
164 | - |
|
165 | - $sql = 'SELECT * FROM '.$connection->quoteIdentifier($this->dbTableName).' WHERE '.$sql_where; |
|
166 | - $result = $connection->executeQuery($sql, $parameters); |
|
167 | - |
|
168 | - if ($result->rowCount() === 0) { |
|
169 | - throw new TDBMException("Could not retrieve object from table \"$this->dbTableName\" using filter \"\"."); |
|
170 | - } |
|
171 | - |
|
172 | - $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
173 | - |
|
174 | - $this->dbRow = []; |
|
175 | - $types = $this->tdbmService->_getColumnTypesForTable($this->dbTableName); |
|
176 | - |
|
177 | - foreach ($row as $key => $value) { |
|
178 | - $this->dbRow[$key] = $types[$key]->convertToPHPValue($value, $connection->getDatabasePlatform()); |
|
179 | - } |
|
180 | - |
|
181 | - $result->closeCursor(); |
|
182 | - |
|
183 | - $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
184 | - } |
|
185 | - } |
|
186 | - |
|
187 | - public function get($var) |
|
188 | - { |
|
189 | - $this->_dbLoadIfNotLoaded(); |
|
190 | - |
|
191 | - // Let's first check if the key exist. |
|
192 | - if (!isset($this->dbRow[$var])) { |
|
193 | - /* |
|
30 | + /** |
|
31 | + * The service this object is bound to. |
|
32 | + * |
|
33 | + * @var TDBMService |
|
34 | + */ |
|
35 | + protected $tdbmService; |
|
36 | + |
|
37 | + /** |
|
38 | + * The object containing this db row. |
|
39 | + * |
|
40 | + * @var AbstractTDBMObject |
|
41 | + */ |
|
42 | + private $object; |
|
43 | + |
|
44 | + /** |
|
45 | + * The name of the table the object if issued from. |
|
46 | + * |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + private $dbTableName; |
|
50 | + |
|
51 | + /** |
|
52 | + * The array of columns returned from database. |
|
53 | + * |
|
54 | + * @var array |
|
55 | + */ |
|
56 | + private $dbRow = array(); |
|
57 | + |
|
58 | + /** |
|
59 | + * @var AbstractTDBMObject[] |
|
60 | + */ |
|
61 | + private $references = array(); |
|
62 | + |
|
63 | + /** |
|
64 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
65 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
66 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
67 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
68 | + * |
|
69 | + * @var string |
|
70 | + */ |
|
71 | + private $status; |
|
72 | + |
|
73 | + /** |
|
74 | + * The values of the primary key. |
|
75 | + * This is set when the object is in "loaded" state. |
|
76 | + * |
|
77 | + * @var array An array of column => value |
|
78 | + */ |
|
79 | + private $primaryKeys; |
|
80 | + |
|
81 | + /** |
|
82 | + * You should never call the constructor directly. Instead, you should use the |
|
83 | + * TDBMService class that will create TDBMObjects for you. |
|
84 | + * |
|
85 | + * Used with id!=false when we want to retrieve an existing object |
|
86 | + * and id==false if we want a new object |
|
87 | + * |
|
88 | + * @param AbstractTDBMObject $object The object containing this db row |
|
89 | + * @param string $table_name |
|
90 | + * @param array $primaryKeys |
|
91 | + * @param TDBMService $tdbmService |
|
92 | + * |
|
93 | + * @throws TDBMException |
|
94 | + * @throws TDBMInvalidOperationException |
|
95 | + */ |
|
96 | + public function __construct(AbstractTDBMObject $object, $table_name, array $primaryKeys = array(), TDBMService $tdbmService = null, array $dbRow = array()) |
|
97 | + { |
|
98 | + $this->object = $object; |
|
99 | + $this->dbTableName = $table_name; |
|
100 | + |
|
101 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
102 | + |
|
103 | + if ($tdbmService === null) { |
|
104 | + if (!empty($primaryKeys)) { |
|
105 | + throw new TDBMException('You cannot pass an id to the DbRow constructor without passing also a TDBMService.'); |
|
106 | + } |
|
107 | + } else { |
|
108 | + $this->tdbmService = $tdbmService; |
|
109 | + |
|
110 | + if (!empty($primaryKeys)) { |
|
111 | + $this->_setPrimaryKeys($primaryKeys); |
|
112 | + if (!empty($dbRow)) { |
|
113 | + $this->dbRow = $dbRow; |
|
114 | + $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
115 | + } else { |
|
116 | + $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
117 | + } |
|
118 | + $tdbmService->_addToCache($this); |
|
119 | + } else { |
|
120 | + $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
121 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
122 | + } |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + public function _attach(TDBMService $tdbmService) |
|
127 | + { |
|
128 | + if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
129 | + throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
130 | + } |
|
131 | + $this->tdbmService = $tdbmService; |
|
132 | + $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
133 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Sets the state of the TDBM Object |
|
138 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
139 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
140 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
141 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
142 | + * |
|
143 | + * @param string $state |
|
144 | + */ |
|
145 | + public function _setStatus($state) |
|
146 | + { |
|
147 | + $this->status = $state; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * This is an internal method. You should not call this method yourself. The TDBM library will do it for you. |
|
152 | + * If the object is in state 'not loaded', this method performs a query in database to load the object. |
|
153 | + * |
|
154 | + * A TDBMException is thrown is no object can be retrieved (for instance, if the primary key specified |
|
155 | + * cannot be found). |
|
156 | + */ |
|
157 | + public function _dbLoadIfNotLoaded() |
|
158 | + { |
|
159 | + if ($this->status == TDBMObjectStateEnum::STATE_NOT_LOADED) { |
|
160 | + $connection = $this->tdbmService->getConnection(); |
|
161 | + |
|
162 | + /// buildFilterFromFilterBag($filter_bag) |
|
163 | + list($sql_where, $parameters) = $this->tdbmService->buildFilterFromFilterBag($this->primaryKeys); |
|
164 | + |
|
165 | + $sql = 'SELECT * FROM '.$connection->quoteIdentifier($this->dbTableName).' WHERE '.$sql_where; |
|
166 | + $result = $connection->executeQuery($sql, $parameters); |
|
167 | + |
|
168 | + if ($result->rowCount() === 0) { |
|
169 | + throw new TDBMException("Could not retrieve object from table \"$this->dbTableName\" using filter \"\"."); |
|
170 | + } |
|
171 | + |
|
172 | + $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
173 | + |
|
174 | + $this->dbRow = []; |
|
175 | + $types = $this->tdbmService->_getColumnTypesForTable($this->dbTableName); |
|
176 | + |
|
177 | + foreach ($row as $key => $value) { |
|
178 | + $this->dbRow[$key] = $types[$key]->convertToPHPValue($value, $connection->getDatabasePlatform()); |
|
179 | + } |
|
180 | + |
|
181 | + $result->closeCursor(); |
|
182 | + |
|
183 | + $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
184 | + } |
|
185 | + } |
|
186 | + |
|
187 | + public function get($var) |
|
188 | + { |
|
189 | + $this->_dbLoadIfNotLoaded(); |
|
190 | + |
|
191 | + // Let's first check if the key exist. |
|
192 | + if (!isset($this->dbRow[$var])) { |
|
193 | + /* |
|
194 | 194 | // Unable to find column.... this is an error if the object has been retrieved from database. |
195 | 195 | // If it's a new object, well, that may not be an error after all! |
196 | 196 | // Let's check if the column does exist in the table |
@@ -210,39 +210,39 @@ discard block |
||
210 | 210 | $str = "Could not find column \"$var\" in table \"$this->dbTableName\". Maybe you meant one of those columns: '".implode("', '",$result_array)."'"; |
211 | 211 | |
212 | 212 | throw new TDBMException($str);*/ |
213 | - return; |
|
214 | - } |
|
215 | - |
|
216 | - $value = $this->dbRow[$var]; |
|
217 | - if ($value instanceof \DateTime) { |
|
218 | - if (method_exists('DateTimeImmutable', 'createFromMutable')) { // PHP 5.6+ only |
|
219 | - return \DateTimeImmutable::createFromMutable($value); |
|
220 | - } else { |
|
221 | - return new \DateTimeImmutable($value->format('c')); |
|
222 | - } |
|
223 | - } |
|
224 | - |
|
225 | - return $this->dbRow[$var]; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Returns true if a column is set, false otherwise. |
|
230 | - * |
|
231 | - * @param string $var |
|
232 | - * |
|
233 | - * @return bool |
|
234 | - */ |
|
235 | - /*public function has($var) { |
|
213 | + return; |
|
214 | + } |
|
215 | + |
|
216 | + $value = $this->dbRow[$var]; |
|
217 | + if ($value instanceof \DateTime) { |
|
218 | + if (method_exists('DateTimeImmutable', 'createFromMutable')) { // PHP 5.6+ only |
|
219 | + return \DateTimeImmutable::createFromMutable($value); |
|
220 | + } else { |
|
221 | + return new \DateTimeImmutable($value->format('c')); |
|
222 | + } |
|
223 | + } |
|
224 | + |
|
225 | + return $this->dbRow[$var]; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Returns true if a column is set, false otherwise. |
|
230 | + * |
|
231 | + * @param string $var |
|
232 | + * |
|
233 | + * @return bool |
|
234 | + */ |
|
235 | + /*public function has($var) { |
|
236 | 236 | $this->_dbLoadIfNotLoaded(); |
237 | 237 | |
238 | 238 | return isset($this->dbRow[$var]); |
239 | 239 | }*/ |
240 | 240 | |
241 | - public function set($var, $value) |
|
242 | - { |
|
243 | - $this->_dbLoadIfNotLoaded(); |
|
241 | + public function set($var, $value) |
|
242 | + { |
|
243 | + $this->_dbLoadIfNotLoaded(); |
|
244 | 244 | |
245 | - /* |
|
245 | + /* |
|
246 | 246 | // Ok, let's start by checking the column type |
247 | 247 | $type = $this->db_connection->getColumnType($this->dbTableName, $var); |
248 | 248 | |
@@ -252,198 +252,198 @@ discard block |
||
252 | 252 | } |
253 | 253 | */ |
254 | 254 | |
255 | - /*if ($var == $this->getPrimaryKey() && isset($this->dbRow[$var])) |
|
255 | + /*if ($var == $this->getPrimaryKey() && isset($this->dbRow[$var])) |
|
256 | 256 | throw new TDBMException("Error! Changing primary key value is forbidden.");*/ |
257 | - $this->dbRow[$var] = $value; |
|
258 | - if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
259 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
260 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
261 | - } |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * @param string $foreignKeyName |
|
266 | - * @param AbstractTDBMObject $bean |
|
267 | - */ |
|
268 | - public function setRef($foreignKeyName, AbstractTDBMObject $bean = null) |
|
269 | - { |
|
270 | - $this->references[$foreignKeyName] = $bean; |
|
271 | - |
|
272 | - if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
273 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
274 | - $this->tdbmService->_addToToSaveObjectList($this); |
|
275 | - } |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @param string $foreignKeyName A unique name for this reference |
|
280 | - * |
|
281 | - * @return AbstractTDBMObject|null |
|
282 | - */ |
|
283 | - public function getRef($foreignKeyName) |
|
284 | - { |
|
285 | - if (array_key_exists($foreignKeyName, $this->references)) { |
|
286 | - return $this->references[$foreignKeyName]; |
|
287 | - } elseif ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->tdbmService === null) { |
|
288 | - // If the object is new and has no property, then it has to be empty. |
|
289 | - return; |
|
290 | - } else { |
|
291 | - $this->_dbLoadIfNotLoaded(); |
|
292 | - |
|
293 | - // Let's match the name of the columns to the primary key values |
|
294 | - $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
295 | - |
|
296 | - $values = []; |
|
297 | - foreach ($fk->getLocalColumns() as $column) { |
|
298 | - if (!isset($this->dbRow[$column])) { |
|
299 | - return; |
|
300 | - } |
|
301 | - $values[] = $this->dbRow[$column]; |
|
302 | - } |
|
303 | - |
|
304 | - $filter = array_combine($this->tdbmService->getPrimaryKeyColumns($fk->getForeignTableName()), $values); |
|
305 | - |
|
306 | - return $this->tdbmService->findObjectByPk($fk->getForeignTableName(), $filter, [], true); |
|
307 | - } |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Returns the name of the table this object comes from. |
|
312 | - * |
|
313 | - * @return string |
|
314 | - */ |
|
315 | - public function _getDbTableName() |
|
316 | - { |
|
317 | - return $this->dbTableName; |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Method used internally by TDBM. You should not use it directly. |
|
322 | - * This method returns the status of the TDBMObject. |
|
323 | - * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
324 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
325 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
326 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
327 | - * |
|
328 | - * @return string |
|
329 | - */ |
|
330 | - public function _getStatus() |
|
331 | - { |
|
332 | - return $this->status; |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Override the native php clone function for TDBMObjects. |
|
337 | - */ |
|
338 | - public function __clone() |
|
339 | - { |
|
340 | - // Let's load the row (before we lose the ID!) |
|
341 | - $this->_dbLoadIfNotLoaded(); |
|
342 | - |
|
343 | - //Let's set the status to detached |
|
344 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
345 | - |
|
346 | - $this->primaryKeys = []; |
|
347 | - |
|
348 | - //Now unset the PK from the row |
|
349 | - if ($this->tdbmService) { |
|
350 | - $pk_array = $this->tdbmService->getPrimaryKeyColumns($this->dbTableName); |
|
351 | - foreach ($pk_array as $pk) { |
|
352 | - $this->dbRow[$pk] = null; |
|
353 | - } |
|
354 | - } |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Returns raw database row. |
|
359 | - * |
|
360 | - * @return array |
|
361 | - * |
|
362 | - * @throws TDBMMissingReferenceException |
|
363 | - */ |
|
364 | - public function _getDbRow() |
|
365 | - { |
|
366 | - // Let's merge $dbRow and $references |
|
367 | - $dbRow = $this->dbRow; |
|
368 | - |
|
369 | - foreach ($this->references as $foreignKeyName => $reference) { |
|
370 | - // Let's match the name of the columns to the primary key values |
|
371 | - $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
372 | - $localColumns = $fk->getLocalColumns(); |
|
373 | - |
|
374 | - if ($reference !== null) { |
|
375 | - $refDbRows = $reference->_getDbRows(); |
|
376 | - $firstRefDbRow = reset($refDbRows); |
|
377 | - if ($firstRefDbRow->_getStatus() == TDBMObjectStateEnum::STATE_DELETED) { |
|
378 | - throw TDBMMissingReferenceException::referenceDeleted($this->dbTableName, $reference); |
|
379 | - } |
|
380 | - $pkValues = array_values($firstRefDbRow->_getPrimaryKeys()); |
|
381 | - for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
382 | - $dbRow[$localColumns[$i]] = $pkValues[$i]; |
|
383 | - } |
|
384 | - } else { |
|
385 | - for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
386 | - $dbRow[$localColumns[$i]] = null; |
|
387 | - } |
|
388 | - } |
|
389 | - } |
|
390 | - |
|
391 | - return $dbRow; |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * Returns references array. |
|
396 | - * |
|
397 | - * @return AbstractTDBMObject[] |
|
398 | - */ |
|
399 | - public function _getReferences() |
|
400 | - { |
|
401 | - return $this->references; |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * Returns the values of the primary key. |
|
406 | - * This is set when the object is in "loaded" state. |
|
407 | - * |
|
408 | - * @return array |
|
409 | - */ |
|
410 | - public function _getPrimaryKeys() |
|
411 | - { |
|
412 | - return $this->primaryKeys; |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * Sets the values of the primary key. |
|
417 | - * This is set when the object is in "loaded" state. |
|
418 | - * |
|
419 | - * @param array $primaryKeys |
|
420 | - */ |
|
421 | - public function _setPrimaryKeys(array $primaryKeys) |
|
422 | - { |
|
423 | - $this->primaryKeys = $primaryKeys; |
|
424 | - foreach ($this->primaryKeys as $column => $value) { |
|
425 | - $this->dbRow[$column] = $value; |
|
426 | - } |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * Returns the TDBMObject this bean is associated to. |
|
431 | - * |
|
432 | - * @return AbstractTDBMObject |
|
433 | - */ |
|
434 | - public function getTDBMObject() |
|
435 | - { |
|
436 | - return $this->object; |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * Sets the TDBMObject this bean is associated to. |
|
441 | - * Only used when cloning. |
|
442 | - * |
|
443 | - * @param AbstractTDBMObject $object |
|
444 | - */ |
|
445 | - public function setTDBMObject(AbstractTDBMObject $object) |
|
446 | - { |
|
447 | - $this->object = $object; |
|
448 | - } |
|
257 | + $this->dbRow[$var] = $value; |
|
258 | + if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
259 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
260 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
261 | + } |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * @param string $foreignKeyName |
|
266 | + * @param AbstractTDBMObject $bean |
|
267 | + */ |
|
268 | + public function setRef($foreignKeyName, AbstractTDBMObject $bean = null) |
|
269 | + { |
|
270 | + $this->references[$foreignKeyName] = $bean; |
|
271 | + |
|
272 | + if ($this->tdbmService !== null && $this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
273 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
274 | + $this->tdbmService->_addToToSaveObjectList($this); |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @param string $foreignKeyName A unique name for this reference |
|
280 | + * |
|
281 | + * @return AbstractTDBMObject|null |
|
282 | + */ |
|
283 | + public function getRef($foreignKeyName) |
|
284 | + { |
|
285 | + if (array_key_exists($foreignKeyName, $this->references)) { |
|
286 | + return $this->references[$foreignKeyName]; |
|
287 | + } elseif ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->tdbmService === null) { |
|
288 | + // If the object is new and has no property, then it has to be empty. |
|
289 | + return; |
|
290 | + } else { |
|
291 | + $this->_dbLoadIfNotLoaded(); |
|
292 | + |
|
293 | + // Let's match the name of the columns to the primary key values |
|
294 | + $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
295 | + |
|
296 | + $values = []; |
|
297 | + foreach ($fk->getLocalColumns() as $column) { |
|
298 | + if (!isset($this->dbRow[$column])) { |
|
299 | + return; |
|
300 | + } |
|
301 | + $values[] = $this->dbRow[$column]; |
|
302 | + } |
|
303 | + |
|
304 | + $filter = array_combine($this->tdbmService->getPrimaryKeyColumns($fk->getForeignTableName()), $values); |
|
305 | + |
|
306 | + return $this->tdbmService->findObjectByPk($fk->getForeignTableName(), $filter, [], true); |
|
307 | + } |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Returns the name of the table this object comes from. |
|
312 | + * |
|
313 | + * @return string |
|
314 | + */ |
|
315 | + public function _getDbTableName() |
|
316 | + { |
|
317 | + return $this->dbTableName; |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Method used internally by TDBM. You should not use it directly. |
|
322 | + * This method returns the status of the TDBMObject. |
|
323 | + * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
324 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
325 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
326 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
327 | + * |
|
328 | + * @return string |
|
329 | + */ |
|
330 | + public function _getStatus() |
|
331 | + { |
|
332 | + return $this->status; |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Override the native php clone function for TDBMObjects. |
|
337 | + */ |
|
338 | + public function __clone() |
|
339 | + { |
|
340 | + // Let's load the row (before we lose the ID!) |
|
341 | + $this->_dbLoadIfNotLoaded(); |
|
342 | + |
|
343 | + //Let's set the status to detached |
|
344 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
345 | + |
|
346 | + $this->primaryKeys = []; |
|
347 | + |
|
348 | + //Now unset the PK from the row |
|
349 | + if ($this->tdbmService) { |
|
350 | + $pk_array = $this->tdbmService->getPrimaryKeyColumns($this->dbTableName); |
|
351 | + foreach ($pk_array as $pk) { |
|
352 | + $this->dbRow[$pk] = null; |
|
353 | + } |
|
354 | + } |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Returns raw database row. |
|
359 | + * |
|
360 | + * @return array |
|
361 | + * |
|
362 | + * @throws TDBMMissingReferenceException |
|
363 | + */ |
|
364 | + public function _getDbRow() |
|
365 | + { |
|
366 | + // Let's merge $dbRow and $references |
|
367 | + $dbRow = $this->dbRow; |
|
368 | + |
|
369 | + foreach ($this->references as $foreignKeyName => $reference) { |
|
370 | + // Let's match the name of the columns to the primary key values |
|
371 | + $fk = $this->tdbmService->_getForeignKeyByName($this->dbTableName, $foreignKeyName); |
|
372 | + $localColumns = $fk->getLocalColumns(); |
|
373 | + |
|
374 | + if ($reference !== null) { |
|
375 | + $refDbRows = $reference->_getDbRows(); |
|
376 | + $firstRefDbRow = reset($refDbRows); |
|
377 | + if ($firstRefDbRow->_getStatus() == TDBMObjectStateEnum::STATE_DELETED) { |
|
378 | + throw TDBMMissingReferenceException::referenceDeleted($this->dbTableName, $reference); |
|
379 | + } |
|
380 | + $pkValues = array_values($firstRefDbRow->_getPrimaryKeys()); |
|
381 | + for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
382 | + $dbRow[$localColumns[$i]] = $pkValues[$i]; |
|
383 | + } |
|
384 | + } else { |
|
385 | + for ($i = 0, $count = count($localColumns); $i < $count; ++$i) { |
|
386 | + $dbRow[$localColumns[$i]] = null; |
|
387 | + } |
|
388 | + } |
|
389 | + } |
|
390 | + |
|
391 | + return $dbRow; |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * Returns references array. |
|
396 | + * |
|
397 | + * @return AbstractTDBMObject[] |
|
398 | + */ |
|
399 | + public function _getReferences() |
|
400 | + { |
|
401 | + return $this->references; |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * Returns the values of the primary key. |
|
406 | + * This is set when the object is in "loaded" state. |
|
407 | + * |
|
408 | + * @return array |
|
409 | + */ |
|
410 | + public function _getPrimaryKeys() |
|
411 | + { |
|
412 | + return $this->primaryKeys; |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * Sets the values of the primary key. |
|
417 | + * This is set when the object is in "loaded" state. |
|
418 | + * |
|
419 | + * @param array $primaryKeys |
|
420 | + */ |
|
421 | + public function _setPrimaryKeys(array $primaryKeys) |
|
422 | + { |
|
423 | + $this->primaryKeys = $primaryKeys; |
|
424 | + foreach ($this->primaryKeys as $column => $value) { |
|
425 | + $this->dbRow[$column] = $value; |
|
426 | + } |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * Returns the TDBMObject this bean is associated to. |
|
431 | + * |
|
432 | + * @return AbstractTDBMObject |
|
433 | + */ |
|
434 | + public function getTDBMObject() |
|
435 | + { |
|
436 | + return $this->object; |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * Sets the TDBMObject this bean is associated to. |
|
441 | + * Only used when cloning. |
|
442 | + * |
|
443 | + * @param AbstractTDBMObject $object |
|
444 | + */ |
|
445 | + public function setTDBMObject(AbstractTDBMObject $object) |
|
446 | + { |
|
447 | + $this->object = $object; |
|
448 | + } |
|
449 | 449 | } |
@@ -27,11 +27,11 @@ |
||
27 | 27 | */ |
28 | 28 | final class TDBMObjectStateEnum |
29 | 29 | { |
30 | - const STATE_DETACHED = 'detached'; |
|
31 | - const STATE_NEW = 'new'; |
|
32 | - const STATE_SAVING = 'saving'; |
|
33 | - const STATE_NOT_LOADED = 'not loaded'; |
|
34 | - const STATE_LOADED = 'loaded'; |
|
35 | - const STATE_DIRTY = 'dirty'; |
|
36 | - const STATE_DELETED = 'deleted'; |
|
30 | + const STATE_DETACHED = 'detached'; |
|
31 | + const STATE_NEW = 'new'; |
|
32 | + const STATE_SAVING = 'saving'; |
|
33 | + const STATE_NOT_LOADED = 'not loaded'; |
|
34 | + const STATE_LOADED = 'loaded'; |
|
35 | + const STATE_DIRTY = 'dirty'; |
|
36 | + const STATE_DELETED = 'deleted'; |
|
37 | 37 | } |