@@ -19,320 +19,320 @@ |
||
19 | 19 | */ |
20 | 20 | abstract class AbstractActiveRecord implements ActiveRecordInterface |
21 | 21 | { |
22 | - /** @var \PDO The PDO object. */ |
|
23 | - private $pdo; |
|
24 | - |
|
25 | - /** @var null|int The ID. */ |
|
26 | - private $id; |
|
27 | - |
|
28 | - /** |
|
29 | - * Construct an abstract active record with the given PDO. |
|
30 | - * |
|
31 | - * @param \PDO $pdo |
|
32 | - */ |
|
33 | - public function __construct(\PDO $pdo) |
|
34 | - { |
|
35 | - $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC); |
|
36 | - $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
37 | - |
|
38 | - $this->setPdo($pdo); |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * {@inheritdoc} |
|
43 | - */ |
|
44 | - public function create() |
|
45 | - { |
|
46 | - try { |
|
47 | - (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
48 | - ->insert($this->getActiveRecordColumns()) |
|
49 | - ->execute(); |
|
50 | - |
|
51 | - $this->setId(intval($this->getPdo()->lastInsertId())); |
|
52 | - } catch (\PDOException $e) { |
|
53 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
54 | - } |
|
55 | - |
|
56 | - return $this; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * {@inheritdoc} |
|
61 | - */ |
|
62 | - public function read($id) |
|
63 | - { |
|
64 | - try { |
|
65 | - $result = (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
66 | - ->select() |
|
67 | - ->where('id', '=', $id) |
|
68 | - ->execute() |
|
69 | - ->fetch(); |
|
70 | - |
|
71 | - if ($result === false) { |
|
72 | - throw new ActiveRecordException(sprintf('Can not read the non-existent active record entry %d from the `%s` table.', $id, $this->getActiveRecordTable())); |
|
73 | - } |
|
74 | - |
|
75 | - $this->fill($result); |
|
76 | - } catch (\PDOException $e) { |
|
77 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
78 | - } |
|
79 | - |
|
80 | - return $this; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * {@inheritdoc} |
|
85 | - */ |
|
86 | - public function update() |
|
87 | - { |
|
88 | - if (!$this->exists()) { |
|
89 | - throw new ActiveRecordException(sprintf('Can not update a non-existent active record entry to the `%s` table.', $this->getActiveRecordTable())); |
|
90 | - } |
|
91 | - |
|
92 | - try { |
|
93 | - (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
94 | - ->update($this->getActiveRecordColumns()) |
|
95 | - ->where('id', '=', $this->getId()) |
|
96 | - ->execute(); |
|
97 | - } catch (\PDOException $e) { |
|
98 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
99 | - } |
|
100 | - |
|
101 | - return $this; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * {@inheritdoc} |
|
106 | - */ |
|
107 | - public function delete() |
|
108 | - { |
|
109 | - if (!$this->exists()) { |
|
110 | - throw new ActiveRecordException(sprintf('Can not delete a non-existent active record entry from the `%s` table.', $this->getActiveRecordTable())); |
|
111 | - } |
|
112 | - |
|
113 | - try { |
|
114 | - (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
115 | - ->delete() |
|
116 | - ->where('id', '=', $this->getId()) |
|
117 | - ->execute(); |
|
118 | - |
|
119 | - $this->setId(null); |
|
120 | - } catch (\PDOException $e) { |
|
121 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
122 | - } |
|
123 | - |
|
124 | - return $this; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * {@inheritdoc} |
|
129 | - */ |
|
130 | - public function sync() |
|
131 | - { |
|
132 | - if (!$this->exists()) { |
|
133 | - return $this->create(); |
|
134 | - } |
|
135 | - |
|
136 | - return $this->update(); |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * {@inheritdoc} |
|
141 | - */ |
|
142 | - public function exists() |
|
143 | - { |
|
144 | - return $this->getId() !== null; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * {@inheritdoc} |
|
149 | - */ |
|
150 | - public function fill(array $attributes) |
|
151 | - { |
|
152 | - if (isset($attributes['id'])) { |
|
153 | - $this->setId($attributes['id']); |
|
154 | - } |
|
155 | - |
|
156 | - $columns = $this->getActiveRecordColumns(); |
|
157 | - |
|
158 | - foreach ($columns as $key => &$value) { |
|
159 | - if (!array_key_exists($key, $attributes)) { |
|
160 | - throw new ActiveRecordException(sprintf('Can not read the expected column `%s`. It\'s not returnd by the `%s` table', $key, $this->getActiveRecordTable())); |
|
161 | - } |
|
162 | - |
|
163 | - $value = $attributes[$key]; |
|
164 | - } |
|
165 | - |
|
166 | - return $this; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * {@inheritdoc} |
|
171 | - */ |
|
172 | - public function searchOne(array $where = [], array $orderBy = []) |
|
173 | - { |
|
174 | - try { |
|
175 | - $result = $this->getSearchQueryResult($where, $orderBy, 1)->fetch(); |
|
176 | - |
|
177 | - if ($result === false) { |
|
178 | - throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->getActiveRecordTable())); |
|
179 | - } |
|
180 | - |
|
181 | - return $this->fill($result); |
|
182 | - } catch (\PDOException $e) { |
|
183 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
184 | - } |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * {@inheritdoc} |
|
189 | - */ |
|
190 | - public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0) |
|
191 | - { |
|
192 | - try { |
|
193 | - $queryResult = $this->getSearchQueryResult($where, $orderBy, $limit, $offset); |
|
194 | - $result = []; |
|
195 | - |
|
196 | - foreach ($queryResult as $fetch) { |
|
197 | - $new = clone $this; |
|
198 | - |
|
199 | - $result[] = $new->fill($fetch); |
|
200 | - } |
|
201 | - |
|
202 | - return $result; |
|
203 | - } catch (\PDOException $e) { |
|
204 | - throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
205 | - } |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Returns the search query result with the given where, order by, limit and offset clauses. |
|
210 | - * |
|
211 | - * @param array $where = [] |
|
212 | - * @param array $orderBy = [] |
|
213 | - * @param int $limit = -1 |
|
214 | - * @param int $offset = 0 |
|
215 | - * @return \miBadger\Query\QueryResult the search query result with the given where, order by, limit and offset clauses. |
|
216 | - */ |
|
217 | - private function getSearchQueryResult(array $where = [], array $orderBy = [], $limit = -1, $offset = 0) |
|
218 | - { |
|
219 | - $query = (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
220 | - ->select(); |
|
221 | - |
|
222 | - $this->getSearchQueryWhere($query, $where); |
|
223 | - $this->getSearchQueryOrderBy($query, $orderBy); |
|
224 | - $this->getSearchQueryLimit($query, $limit, $offset); |
|
225 | - |
|
226 | - return $query->execute(); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Returns the given query after adding the given where conditions. |
|
231 | - * |
|
232 | - * @param \miBadger\Query\Query $query |
|
233 | - * @param array $where |
|
234 | - * @return \miBadger\Query\Query the given query after adding the given where conditions. |
|
235 | - */ |
|
236 | - private function getSearchQueryWhere($query, $where) |
|
237 | - { |
|
238 | - foreach ($where as $key => $value) { |
|
239 | - $query->where($value[0], $value[1], $value[2]); |
|
240 | - } |
|
241 | - |
|
242 | - return $query; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Returns the given query after adding the given order by conditions. |
|
247 | - * |
|
248 | - * @param \miBadger\Query\Query $query |
|
249 | - * @param array $orderBy |
|
250 | - * @return \miBadger\Query\Query the given query after adding the given order by conditions. |
|
251 | - */ |
|
252 | - private function getSearchQueryOrderBy($query, $orderBy) |
|
253 | - { |
|
254 | - foreach ($orderBy as $key => $value) { |
|
255 | - $query->orderBy($key, $value); |
|
256 | - } |
|
257 | - |
|
258 | - return $query; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Returns the given query after adding the given limit and offset conditions. |
|
263 | - * |
|
264 | - * @param \miBadger\Query\Query $query |
|
265 | - * @param int $limit |
|
266 | - * @param int $offset |
|
267 | - * @return \miBadger\Query\Query the given query after adding the given limit and offset conditions. |
|
268 | - */ |
|
269 | - private function getSearchQueryLimit($query, $limit, $offset) |
|
270 | - { |
|
271 | - if ($limit > -1) { |
|
272 | - $query->limit($limit); |
|
273 | - $query->offset($offset); |
|
274 | - } |
|
275 | - |
|
276 | - return $query; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Returns the PDO. |
|
281 | - * |
|
282 | - * @return \PDO the PDO. |
|
283 | - */ |
|
284 | - public function getPdo() |
|
285 | - { |
|
286 | - return $this->pdo; |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Set the PDO. |
|
291 | - * |
|
292 | - * @param \PDO $pdo |
|
293 | - * @return $this |
|
294 | - */ |
|
295 | - protected function setPdo($pdo) |
|
296 | - { |
|
297 | - $this->pdo = $pdo; |
|
298 | - |
|
299 | - return $this; |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * Returns the ID. |
|
304 | - * |
|
305 | - * @return null|int The ID. |
|
306 | - */ |
|
307 | - public function getId() |
|
308 | - { |
|
309 | - return $this->id; |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * Set the ID. |
|
314 | - * |
|
315 | - * @param int $id |
|
316 | - * @return $this |
|
317 | - */ |
|
318 | - protected function setId($id) |
|
319 | - { |
|
320 | - $this->id = $id; |
|
321 | - |
|
322 | - return $this; |
|
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * Returns the active record table. |
|
327 | - * |
|
328 | - * @return string the active record table. |
|
329 | - */ |
|
330 | - abstract protected function getActiveRecordTable(); |
|
331 | - |
|
332 | - /** |
|
333 | - * Returns the active record columns. |
|
334 | - * |
|
335 | - * @return array the active record columns. |
|
336 | - */ |
|
337 | - abstract protected function getActiveRecordColumns(); |
|
22 | + /** @var \PDO The PDO object. */ |
|
23 | + private $pdo; |
|
24 | + |
|
25 | + /** @var null|int The ID. */ |
|
26 | + private $id; |
|
27 | + |
|
28 | + /** |
|
29 | + * Construct an abstract active record with the given PDO. |
|
30 | + * |
|
31 | + * @param \PDO $pdo |
|
32 | + */ |
|
33 | + public function __construct(\PDO $pdo) |
|
34 | + { |
|
35 | + $pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC); |
|
36 | + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
37 | + |
|
38 | + $this->setPdo($pdo); |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * {@inheritdoc} |
|
43 | + */ |
|
44 | + public function create() |
|
45 | + { |
|
46 | + try { |
|
47 | + (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
48 | + ->insert($this->getActiveRecordColumns()) |
|
49 | + ->execute(); |
|
50 | + |
|
51 | + $this->setId(intval($this->getPdo()->lastInsertId())); |
|
52 | + } catch (\PDOException $e) { |
|
53 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
54 | + } |
|
55 | + |
|
56 | + return $this; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * {@inheritdoc} |
|
61 | + */ |
|
62 | + public function read($id) |
|
63 | + { |
|
64 | + try { |
|
65 | + $result = (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
66 | + ->select() |
|
67 | + ->where('id', '=', $id) |
|
68 | + ->execute() |
|
69 | + ->fetch(); |
|
70 | + |
|
71 | + if ($result === false) { |
|
72 | + throw new ActiveRecordException(sprintf('Can not read the non-existent active record entry %d from the `%s` table.', $id, $this->getActiveRecordTable())); |
|
73 | + } |
|
74 | + |
|
75 | + $this->fill($result); |
|
76 | + } catch (\PDOException $e) { |
|
77 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
78 | + } |
|
79 | + |
|
80 | + return $this; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * {@inheritdoc} |
|
85 | + */ |
|
86 | + public function update() |
|
87 | + { |
|
88 | + if (!$this->exists()) { |
|
89 | + throw new ActiveRecordException(sprintf('Can not update a non-existent active record entry to the `%s` table.', $this->getActiveRecordTable())); |
|
90 | + } |
|
91 | + |
|
92 | + try { |
|
93 | + (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
94 | + ->update($this->getActiveRecordColumns()) |
|
95 | + ->where('id', '=', $this->getId()) |
|
96 | + ->execute(); |
|
97 | + } catch (\PDOException $e) { |
|
98 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
99 | + } |
|
100 | + |
|
101 | + return $this; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * {@inheritdoc} |
|
106 | + */ |
|
107 | + public function delete() |
|
108 | + { |
|
109 | + if (!$this->exists()) { |
|
110 | + throw new ActiveRecordException(sprintf('Can not delete a non-existent active record entry from the `%s` table.', $this->getActiveRecordTable())); |
|
111 | + } |
|
112 | + |
|
113 | + try { |
|
114 | + (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
115 | + ->delete() |
|
116 | + ->where('id', '=', $this->getId()) |
|
117 | + ->execute(); |
|
118 | + |
|
119 | + $this->setId(null); |
|
120 | + } catch (\PDOException $e) { |
|
121 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
122 | + } |
|
123 | + |
|
124 | + return $this; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * {@inheritdoc} |
|
129 | + */ |
|
130 | + public function sync() |
|
131 | + { |
|
132 | + if (!$this->exists()) { |
|
133 | + return $this->create(); |
|
134 | + } |
|
135 | + |
|
136 | + return $this->update(); |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * {@inheritdoc} |
|
141 | + */ |
|
142 | + public function exists() |
|
143 | + { |
|
144 | + return $this->getId() !== null; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * {@inheritdoc} |
|
149 | + */ |
|
150 | + public function fill(array $attributes) |
|
151 | + { |
|
152 | + if (isset($attributes['id'])) { |
|
153 | + $this->setId($attributes['id']); |
|
154 | + } |
|
155 | + |
|
156 | + $columns = $this->getActiveRecordColumns(); |
|
157 | + |
|
158 | + foreach ($columns as $key => &$value) { |
|
159 | + if (!array_key_exists($key, $attributes)) { |
|
160 | + throw new ActiveRecordException(sprintf('Can not read the expected column `%s`. It\'s not returnd by the `%s` table', $key, $this->getActiveRecordTable())); |
|
161 | + } |
|
162 | + |
|
163 | + $value = $attributes[$key]; |
|
164 | + } |
|
165 | + |
|
166 | + return $this; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * {@inheritdoc} |
|
171 | + */ |
|
172 | + public function searchOne(array $where = [], array $orderBy = []) |
|
173 | + { |
|
174 | + try { |
|
175 | + $result = $this->getSearchQueryResult($where, $orderBy, 1)->fetch(); |
|
176 | + |
|
177 | + if ($result === false) { |
|
178 | + throw new ActiveRecordException(sprintf('Can not search one non-existent entry from the `%s` table.', $this->getActiveRecordTable())); |
|
179 | + } |
|
180 | + |
|
181 | + return $this->fill($result); |
|
182 | + } catch (\PDOException $e) { |
|
183 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
184 | + } |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * {@inheritdoc} |
|
189 | + */ |
|
190 | + public function search(array $where = [], array $orderBy = [], $limit = -1, $offset = 0) |
|
191 | + { |
|
192 | + try { |
|
193 | + $queryResult = $this->getSearchQueryResult($where, $orderBy, $limit, $offset); |
|
194 | + $result = []; |
|
195 | + |
|
196 | + foreach ($queryResult as $fetch) { |
|
197 | + $new = clone $this; |
|
198 | + |
|
199 | + $result[] = $new->fill($fetch); |
|
200 | + } |
|
201 | + |
|
202 | + return $result; |
|
203 | + } catch (\PDOException $e) { |
|
204 | + throw new ActiveRecordException($e->getMessage(), 0, $e); |
|
205 | + } |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Returns the search query result with the given where, order by, limit and offset clauses. |
|
210 | + * |
|
211 | + * @param array $where = [] |
|
212 | + * @param array $orderBy = [] |
|
213 | + * @param int $limit = -1 |
|
214 | + * @param int $offset = 0 |
|
215 | + * @return \miBadger\Query\QueryResult the search query result with the given where, order by, limit and offset clauses. |
|
216 | + */ |
|
217 | + private function getSearchQueryResult(array $where = [], array $orderBy = [], $limit = -1, $offset = 0) |
|
218 | + { |
|
219 | + $query = (new Query($this->getPdo(), $this->getActiveRecordTable())) |
|
220 | + ->select(); |
|
221 | + |
|
222 | + $this->getSearchQueryWhere($query, $where); |
|
223 | + $this->getSearchQueryOrderBy($query, $orderBy); |
|
224 | + $this->getSearchQueryLimit($query, $limit, $offset); |
|
225 | + |
|
226 | + return $query->execute(); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Returns the given query after adding the given where conditions. |
|
231 | + * |
|
232 | + * @param \miBadger\Query\Query $query |
|
233 | + * @param array $where |
|
234 | + * @return \miBadger\Query\Query the given query after adding the given where conditions. |
|
235 | + */ |
|
236 | + private function getSearchQueryWhere($query, $where) |
|
237 | + { |
|
238 | + foreach ($where as $key => $value) { |
|
239 | + $query->where($value[0], $value[1], $value[2]); |
|
240 | + } |
|
241 | + |
|
242 | + return $query; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Returns the given query after adding the given order by conditions. |
|
247 | + * |
|
248 | + * @param \miBadger\Query\Query $query |
|
249 | + * @param array $orderBy |
|
250 | + * @return \miBadger\Query\Query the given query after adding the given order by conditions. |
|
251 | + */ |
|
252 | + private function getSearchQueryOrderBy($query, $orderBy) |
|
253 | + { |
|
254 | + foreach ($orderBy as $key => $value) { |
|
255 | + $query->orderBy($key, $value); |
|
256 | + } |
|
257 | + |
|
258 | + return $query; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Returns the given query after adding the given limit and offset conditions. |
|
263 | + * |
|
264 | + * @param \miBadger\Query\Query $query |
|
265 | + * @param int $limit |
|
266 | + * @param int $offset |
|
267 | + * @return \miBadger\Query\Query the given query after adding the given limit and offset conditions. |
|
268 | + */ |
|
269 | + private function getSearchQueryLimit($query, $limit, $offset) |
|
270 | + { |
|
271 | + if ($limit > -1) { |
|
272 | + $query->limit($limit); |
|
273 | + $query->offset($offset); |
|
274 | + } |
|
275 | + |
|
276 | + return $query; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Returns the PDO. |
|
281 | + * |
|
282 | + * @return \PDO the PDO. |
|
283 | + */ |
|
284 | + public function getPdo() |
|
285 | + { |
|
286 | + return $this->pdo; |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Set the PDO. |
|
291 | + * |
|
292 | + * @param \PDO $pdo |
|
293 | + * @return $this |
|
294 | + */ |
|
295 | + protected function setPdo($pdo) |
|
296 | + { |
|
297 | + $this->pdo = $pdo; |
|
298 | + |
|
299 | + return $this; |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * Returns the ID. |
|
304 | + * |
|
305 | + * @return null|int The ID. |
|
306 | + */ |
|
307 | + public function getId() |
|
308 | + { |
|
309 | + return $this->id; |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * Set the ID. |
|
314 | + * |
|
315 | + * @param int $id |
|
316 | + * @return $this |
|
317 | + */ |
|
318 | + protected function setId($id) |
|
319 | + { |
|
320 | + $this->id = $id; |
|
321 | + |
|
322 | + return $this; |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * Returns the active record table. |
|
327 | + * |
|
328 | + * @return string the active record table. |
|
329 | + */ |
|
330 | + abstract protected function getActiveRecordTable(); |
|
331 | + |
|
332 | + /** |
|
333 | + * Returns the active record columns. |
|
334 | + * |
|
335 | + * @return array the active record columns. |
|
336 | + */ |
|
337 | + abstract protected function getActiveRecordColumns(); |
|
338 | 338 | } |