@@ -36,289 +36,289 @@ |
||
36 | 36 | */ |
37 | 37 | abstract class QBMapper { |
38 | 38 | |
39 | - /** @var string */ |
|
40 | - protected $tableName; |
|
41 | - |
|
42 | - /** @var string */ |
|
43 | - protected $entityClass; |
|
44 | - |
|
45 | - /** @var IDBConnection */ |
|
46 | - protected $db; |
|
47 | - |
|
48 | - /** |
|
49 | - * @param IDBConnection $db Instance of the Db abstraction layer |
|
50 | - * @param string $tableName the name of the table. set this to allow entity |
|
51 | - * @param string $entityClass the name of the entity that the sql should be |
|
52 | - * mapped to queries without using sql |
|
53 | - * @since 14.0.0 |
|
54 | - */ |
|
55 | - public function __construct(IDBConnection $db, string $tableName, string $entityClass=null){ |
|
56 | - $this->db = $db; |
|
57 | - $this->tableName = $tableName; |
|
58 | - |
|
59 | - // if not given set the entity name to the class without the mapper part |
|
60 | - // cache it here for later use since reflection is slow |
|
61 | - if($entityClass === null) { |
|
62 | - $this->entityClass = str_replace('Mapper', '', \get_class($this)); |
|
63 | - } else { |
|
64 | - $this->entityClass = $entityClass; |
|
65 | - } |
|
66 | - } |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * @return string the table name |
|
71 | - * @since 14.0.0 |
|
72 | - */ |
|
73 | - public function getTableName(): string { |
|
74 | - return $this->tableName; |
|
75 | - } |
|
76 | - |
|
77 | - |
|
78 | - /** |
|
79 | - * Deletes an entity from the table |
|
80 | - * @param Entity $entity the entity that should be deleted |
|
81 | - * @return Entity the deleted entity |
|
82 | - * @since 14.0.0 |
|
83 | - */ |
|
84 | - public function delete(Entity $entity): Entity { |
|
85 | - $qb = $this->db->getQueryBuilder(); |
|
86 | - |
|
87 | - $qb->delete($this->tableName) |
|
88 | - ->where( |
|
89 | - $qb->expr()->eq('id', $qb->createNamedParameter($entity->getId())) |
|
90 | - ); |
|
91 | - $qb->execute(); |
|
92 | - return $entity; |
|
93 | - } |
|
94 | - |
|
95 | - |
|
96 | - /** |
|
97 | - * Creates a new entry in the db from an entity |
|
98 | - * @param Entity $entity the entity that should be created |
|
99 | - * @return Entity the saved entity with the set id |
|
100 | - * @since 14.0.0 |
|
101 | - * @suppress SqlInjectionChecker |
|
102 | - */ |
|
103 | - public function insert(Entity $entity): Entity { |
|
104 | - // get updated fields to save, fields have to be set using a setter to |
|
105 | - // be saved |
|
106 | - $properties = $entity->getUpdatedFields(); |
|
107 | - |
|
108 | - $qb = $this->db->getQueryBuilder(); |
|
109 | - $qb->insert($this->tableName); |
|
110 | - |
|
111 | - // build the fields |
|
112 | - foreach($properties as $property => $updated) { |
|
113 | - $column = $entity->propertyToColumn($property); |
|
114 | - $getter = 'get' . ucfirst($property); |
|
115 | - $value = $entity->$getter(); |
|
116 | - |
|
117 | - $type = $this->getParameterTypeForProperty($entity, $property); |
|
118 | - $qb->setValue($column, $qb->createNamedParameter($value, $type)); |
|
119 | - } |
|
120 | - |
|
121 | - $qb->execute(); |
|
122 | - |
|
123 | - if($entity->id === null) { |
|
124 | - $entity->setId((int)$qb->getLastInsertId()); |
|
125 | - } |
|
126 | - |
|
127 | - return $entity; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Tries to creates a new entry in the db from an entity and |
|
132 | - * updates an existing entry if duplicate keys are detected |
|
133 | - * by the database |
|
134 | - * |
|
135 | - * @param Entity $entity the entity that should be created/updated |
|
136 | - * @return Entity the saved entity with the (new) id |
|
137 | - * @throws \InvalidArgumentException if entity has no id |
|
138 | - * @since 15.0.0 |
|
139 | - * @suppress SqlInjectionChecker |
|
140 | - */ |
|
141 | - public function insertOrUpdate(Entity $entity): Entity { |
|
142 | - try { |
|
143 | - return $this->insert($entity); |
|
144 | - } catch (UniqueConstraintViolationException $ex) { |
|
145 | - return $this->update($entity); |
|
146 | - } |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Updates an entry in the db from an entity |
|
151 | - * @throws \InvalidArgumentException if entity has no id |
|
152 | - * @param Entity $entity the entity that should be created |
|
153 | - * @return Entity the saved entity with the set id |
|
154 | - * @since 14.0.0 |
|
155 | - * @suppress SqlInjectionChecker |
|
156 | - */ |
|
157 | - public function update(Entity $entity): Entity { |
|
158 | - // if entity wasn't changed it makes no sense to run a db query |
|
159 | - $properties = $entity->getUpdatedFields(); |
|
160 | - if(\count($properties) === 0) { |
|
161 | - return $entity; |
|
162 | - } |
|
163 | - |
|
164 | - // entity needs an id |
|
165 | - $id = $entity->getId(); |
|
166 | - if($id === null){ |
|
167 | - throw new \InvalidArgumentException( |
|
168 | - 'Entity which should be updated has no id'); |
|
169 | - } |
|
170 | - |
|
171 | - // get updated fields to save, fields have to be set using a setter to |
|
172 | - // be saved |
|
173 | - // do not update the id field |
|
174 | - unset($properties['id']); |
|
175 | - |
|
176 | - $qb = $this->db->getQueryBuilder(); |
|
177 | - $qb->update($this->tableName); |
|
178 | - |
|
179 | - // build the fields |
|
180 | - foreach($properties as $property => $updated) { |
|
181 | - $column = $entity->propertyToColumn($property); |
|
182 | - $getter = 'get' . ucfirst($property); |
|
183 | - $value = $entity->$getter(); |
|
184 | - |
|
185 | - $type = $this->getParameterTypeForProperty($entity, $property); |
|
186 | - $qb->set($column, $qb->createNamedParameter($value, $type)); |
|
187 | - } |
|
188 | - |
|
189 | - $qb->where( |
|
190 | - $qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)) |
|
191 | - ); |
|
192 | - $qb->execute(); |
|
193 | - |
|
194 | - return $entity; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Returns the type parameter for the QueryBuilder for a specific property |
|
199 | - * of the $entity |
|
200 | - * |
|
201 | - * @param Entity $entity The entity to get the types from |
|
202 | - * @param string $property The property of $entity to get the type for |
|
203 | - * @return int |
|
204 | - * @since 16.0.0 |
|
205 | - */ |
|
206 | - protected function getParameterTypeForProperty(Entity $entity, string $property): int { |
|
207 | - $types = $entity->getFieldTypes(); |
|
208 | - |
|
209 | - if(!isset($types[ $property ])) { |
|
210 | - return IQueryBuilder::PARAM_STR; |
|
211 | - } |
|
212 | - |
|
213 | - switch($types[ $property ]) { |
|
214 | - case 'int': |
|
215 | - case 'integer': |
|
216 | - return IQueryBuilder::PARAM_INT; |
|
217 | - case 'string': |
|
218 | - return IQueryBuilder::PARAM_STR; |
|
219 | - case 'bool': |
|
220 | - case 'boolean': |
|
221 | - return IQueryBuilder::PARAM_BOOL; |
|
222 | - } |
|
223 | - |
|
224 | - return IQueryBuilder::PARAM_STR; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Returns an db result and throws exceptions when there are more or less |
|
229 | - * results |
|
230 | - * |
|
231 | - * @see findEntity |
|
232 | - * |
|
233 | - * @param IQueryBuilder $query |
|
234 | - * @throws DoesNotExistException if the item does not exist |
|
235 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
236 | - * @return array the result as row |
|
237 | - * @since 14.0.0 |
|
238 | - */ |
|
239 | - protected function findOneQuery(IQueryBuilder $query): array { |
|
240 | - $cursor = $query->execute(); |
|
241 | - |
|
242 | - $row = $cursor->fetch(); |
|
243 | - if($row === false) { |
|
244 | - $cursor->closeCursor(); |
|
245 | - $msg = $this->buildDebugMessage( |
|
246 | - 'Did expect one result but found none when executing', $query |
|
247 | - ); |
|
248 | - throw new DoesNotExistException($msg); |
|
249 | - } |
|
250 | - |
|
251 | - $row2 = $cursor->fetch(); |
|
252 | - $cursor->closeCursor(); |
|
253 | - if($row2 !== false ) { |
|
254 | - $msg = $this->buildDebugMessage( |
|
255 | - 'Did not expect more than one result when executing', $query |
|
256 | - ); |
|
257 | - throw new MultipleObjectsReturnedException($msg); |
|
258 | - } |
|
259 | - |
|
260 | - return $row; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * @param string $msg |
|
265 | - * @param IQueryBuilder $sql |
|
266 | - * @return string |
|
267 | - * @since 14.0.0 |
|
268 | - */ |
|
269 | - private function buildDebugMessage(string $msg, IQueryBuilder $sql): string { |
|
270 | - return $msg . |
|
271 | - ': query "' . $sql->getSQL() . '"; '; |
|
272 | - } |
|
273 | - |
|
274 | - |
|
275 | - /** |
|
276 | - * Creates an entity from a row. Automatically determines the entity class |
|
277 | - * from the current mapper name (MyEntityMapper -> MyEntity) |
|
278 | - * |
|
279 | - * @param array $row the row which should be converted to an entity |
|
280 | - * @return Entity the entity |
|
281 | - * @since 14.0.0 |
|
282 | - */ |
|
283 | - protected function mapRowToEntity(array $row): Entity { |
|
284 | - return \call_user_func($this->entityClass .'::fromRow', $row); |
|
285 | - } |
|
286 | - |
|
287 | - |
|
288 | - /** |
|
289 | - * Runs a sql query and returns an array of entities |
|
290 | - * |
|
291 | - * @param IQueryBuilder $query |
|
292 | - * @return Entity[] all fetched entities |
|
293 | - * @since 14.0.0 |
|
294 | - */ |
|
295 | - protected function findEntities(IQueryBuilder $query): array { |
|
296 | - $cursor = $query->execute(); |
|
297 | - |
|
298 | - $entities = []; |
|
299 | - |
|
300 | - while($row = $cursor->fetch()){ |
|
301 | - $entities[] = $this->mapRowToEntity($row); |
|
302 | - } |
|
303 | - |
|
304 | - $cursor->closeCursor(); |
|
305 | - |
|
306 | - return $entities; |
|
307 | - } |
|
308 | - |
|
309 | - |
|
310 | - /** |
|
311 | - * Returns an db result and throws exceptions when there are more or less |
|
312 | - * results |
|
313 | - * |
|
314 | - * @param IQueryBuilder $query |
|
315 | - * @throws DoesNotExistException if the item does not exist |
|
316 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
317 | - * @return Entity the entity |
|
318 | - * @since 14.0.0 |
|
319 | - */ |
|
320 | - protected function findEntity(IQueryBuilder $query): Entity { |
|
321 | - return $this->mapRowToEntity($this->findOneQuery($query)); |
|
322 | - } |
|
39 | + /** @var string */ |
|
40 | + protected $tableName; |
|
41 | + |
|
42 | + /** @var string */ |
|
43 | + protected $entityClass; |
|
44 | + |
|
45 | + /** @var IDBConnection */ |
|
46 | + protected $db; |
|
47 | + |
|
48 | + /** |
|
49 | + * @param IDBConnection $db Instance of the Db abstraction layer |
|
50 | + * @param string $tableName the name of the table. set this to allow entity |
|
51 | + * @param string $entityClass the name of the entity that the sql should be |
|
52 | + * mapped to queries without using sql |
|
53 | + * @since 14.0.0 |
|
54 | + */ |
|
55 | + public function __construct(IDBConnection $db, string $tableName, string $entityClass=null){ |
|
56 | + $this->db = $db; |
|
57 | + $this->tableName = $tableName; |
|
58 | + |
|
59 | + // if not given set the entity name to the class without the mapper part |
|
60 | + // cache it here for later use since reflection is slow |
|
61 | + if($entityClass === null) { |
|
62 | + $this->entityClass = str_replace('Mapper', '', \get_class($this)); |
|
63 | + } else { |
|
64 | + $this->entityClass = $entityClass; |
|
65 | + } |
|
66 | + } |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * @return string the table name |
|
71 | + * @since 14.0.0 |
|
72 | + */ |
|
73 | + public function getTableName(): string { |
|
74 | + return $this->tableName; |
|
75 | + } |
|
76 | + |
|
77 | + |
|
78 | + /** |
|
79 | + * Deletes an entity from the table |
|
80 | + * @param Entity $entity the entity that should be deleted |
|
81 | + * @return Entity the deleted entity |
|
82 | + * @since 14.0.0 |
|
83 | + */ |
|
84 | + public function delete(Entity $entity): Entity { |
|
85 | + $qb = $this->db->getQueryBuilder(); |
|
86 | + |
|
87 | + $qb->delete($this->tableName) |
|
88 | + ->where( |
|
89 | + $qb->expr()->eq('id', $qb->createNamedParameter($entity->getId())) |
|
90 | + ); |
|
91 | + $qb->execute(); |
|
92 | + return $entity; |
|
93 | + } |
|
94 | + |
|
95 | + |
|
96 | + /** |
|
97 | + * Creates a new entry in the db from an entity |
|
98 | + * @param Entity $entity the entity that should be created |
|
99 | + * @return Entity the saved entity with the set id |
|
100 | + * @since 14.0.0 |
|
101 | + * @suppress SqlInjectionChecker |
|
102 | + */ |
|
103 | + public function insert(Entity $entity): Entity { |
|
104 | + // get updated fields to save, fields have to be set using a setter to |
|
105 | + // be saved |
|
106 | + $properties = $entity->getUpdatedFields(); |
|
107 | + |
|
108 | + $qb = $this->db->getQueryBuilder(); |
|
109 | + $qb->insert($this->tableName); |
|
110 | + |
|
111 | + // build the fields |
|
112 | + foreach($properties as $property => $updated) { |
|
113 | + $column = $entity->propertyToColumn($property); |
|
114 | + $getter = 'get' . ucfirst($property); |
|
115 | + $value = $entity->$getter(); |
|
116 | + |
|
117 | + $type = $this->getParameterTypeForProperty($entity, $property); |
|
118 | + $qb->setValue($column, $qb->createNamedParameter($value, $type)); |
|
119 | + } |
|
120 | + |
|
121 | + $qb->execute(); |
|
122 | + |
|
123 | + if($entity->id === null) { |
|
124 | + $entity->setId((int)$qb->getLastInsertId()); |
|
125 | + } |
|
126 | + |
|
127 | + return $entity; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Tries to creates a new entry in the db from an entity and |
|
132 | + * updates an existing entry if duplicate keys are detected |
|
133 | + * by the database |
|
134 | + * |
|
135 | + * @param Entity $entity the entity that should be created/updated |
|
136 | + * @return Entity the saved entity with the (new) id |
|
137 | + * @throws \InvalidArgumentException if entity has no id |
|
138 | + * @since 15.0.0 |
|
139 | + * @suppress SqlInjectionChecker |
|
140 | + */ |
|
141 | + public function insertOrUpdate(Entity $entity): Entity { |
|
142 | + try { |
|
143 | + return $this->insert($entity); |
|
144 | + } catch (UniqueConstraintViolationException $ex) { |
|
145 | + return $this->update($entity); |
|
146 | + } |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Updates an entry in the db from an entity |
|
151 | + * @throws \InvalidArgumentException if entity has no id |
|
152 | + * @param Entity $entity the entity that should be created |
|
153 | + * @return Entity the saved entity with the set id |
|
154 | + * @since 14.0.0 |
|
155 | + * @suppress SqlInjectionChecker |
|
156 | + */ |
|
157 | + public function update(Entity $entity): Entity { |
|
158 | + // if entity wasn't changed it makes no sense to run a db query |
|
159 | + $properties = $entity->getUpdatedFields(); |
|
160 | + if(\count($properties) === 0) { |
|
161 | + return $entity; |
|
162 | + } |
|
163 | + |
|
164 | + // entity needs an id |
|
165 | + $id = $entity->getId(); |
|
166 | + if($id === null){ |
|
167 | + throw new \InvalidArgumentException( |
|
168 | + 'Entity which should be updated has no id'); |
|
169 | + } |
|
170 | + |
|
171 | + // get updated fields to save, fields have to be set using a setter to |
|
172 | + // be saved |
|
173 | + // do not update the id field |
|
174 | + unset($properties['id']); |
|
175 | + |
|
176 | + $qb = $this->db->getQueryBuilder(); |
|
177 | + $qb->update($this->tableName); |
|
178 | + |
|
179 | + // build the fields |
|
180 | + foreach($properties as $property => $updated) { |
|
181 | + $column = $entity->propertyToColumn($property); |
|
182 | + $getter = 'get' . ucfirst($property); |
|
183 | + $value = $entity->$getter(); |
|
184 | + |
|
185 | + $type = $this->getParameterTypeForProperty($entity, $property); |
|
186 | + $qb->set($column, $qb->createNamedParameter($value, $type)); |
|
187 | + } |
|
188 | + |
|
189 | + $qb->where( |
|
190 | + $qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)) |
|
191 | + ); |
|
192 | + $qb->execute(); |
|
193 | + |
|
194 | + return $entity; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Returns the type parameter for the QueryBuilder for a specific property |
|
199 | + * of the $entity |
|
200 | + * |
|
201 | + * @param Entity $entity The entity to get the types from |
|
202 | + * @param string $property The property of $entity to get the type for |
|
203 | + * @return int |
|
204 | + * @since 16.0.0 |
|
205 | + */ |
|
206 | + protected function getParameterTypeForProperty(Entity $entity, string $property): int { |
|
207 | + $types = $entity->getFieldTypes(); |
|
208 | + |
|
209 | + if(!isset($types[ $property ])) { |
|
210 | + return IQueryBuilder::PARAM_STR; |
|
211 | + } |
|
212 | + |
|
213 | + switch($types[ $property ]) { |
|
214 | + case 'int': |
|
215 | + case 'integer': |
|
216 | + return IQueryBuilder::PARAM_INT; |
|
217 | + case 'string': |
|
218 | + return IQueryBuilder::PARAM_STR; |
|
219 | + case 'bool': |
|
220 | + case 'boolean': |
|
221 | + return IQueryBuilder::PARAM_BOOL; |
|
222 | + } |
|
223 | + |
|
224 | + return IQueryBuilder::PARAM_STR; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Returns an db result and throws exceptions when there are more or less |
|
229 | + * results |
|
230 | + * |
|
231 | + * @see findEntity |
|
232 | + * |
|
233 | + * @param IQueryBuilder $query |
|
234 | + * @throws DoesNotExistException if the item does not exist |
|
235 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
236 | + * @return array the result as row |
|
237 | + * @since 14.0.0 |
|
238 | + */ |
|
239 | + protected function findOneQuery(IQueryBuilder $query): array { |
|
240 | + $cursor = $query->execute(); |
|
241 | + |
|
242 | + $row = $cursor->fetch(); |
|
243 | + if($row === false) { |
|
244 | + $cursor->closeCursor(); |
|
245 | + $msg = $this->buildDebugMessage( |
|
246 | + 'Did expect one result but found none when executing', $query |
|
247 | + ); |
|
248 | + throw new DoesNotExistException($msg); |
|
249 | + } |
|
250 | + |
|
251 | + $row2 = $cursor->fetch(); |
|
252 | + $cursor->closeCursor(); |
|
253 | + if($row2 !== false ) { |
|
254 | + $msg = $this->buildDebugMessage( |
|
255 | + 'Did not expect more than one result when executing', $query |
|
256 | + ); |
|
257 | + throw new MultipleObjectsReturnedException($msg); |
|
258 | + } |
|
259 | + |
|
260 | + return $row; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * @param string $msg |
|
265 | + * @param IQueryBuilder $sql |
|
266 | + * @return string |
|
267 | + * @since 14.0.0 |
|
268 | + */ |
|
269 | + private function buildDebugMessage(string $msg, IQueryBuilder $sql): string { |
|
270 | + return $msg . |
|
271 | + ': query "' . $sql->getSQL() . '"; '; |
|
272 | + } |
|
273 | + |
|
274 | + |
|
275 | + /** |
|
276 | + * Creates an entity from a row. Automatically determines the entity class |
|
277 | + * from the current mapper name (MyEntityMapper -> MyEntity) |
|
278 | + * |
|
279 | + * @param array $row the row which should be converted to an entity |
|
280 | + * @return Entity the entity |
|
281 | + * @since 14.0.0 |
|
282 | + */ |
|
283 | + protected function mapRowToEntity(array $row): Entity { |
|
284 | + return \call_user_func($this->entityClass .'::fromRow', $row); |
|
285 | + } |
|
286 | + |
|
287 | + |
|
288 | + /** |
|
289 | + * Runs a sql query and returns an array of entities |
|
290 | + * |
|
291 | + * @param IQueryBuilder $query |
|
292 | + * @return Entity[] all fetched entities |
|
293 | + * @since 14.0.0 |
|
294 | + */ |
|
295 | + protected function findEntities(IQueryBuilder $query): array { |
|
296 | + $cursor = $query->execute(); |
|
297 | + |
|
298 | + $entities = []; |
|
299 | + |
|
300 | + while($row = $cursor->fetch()){ |
|
301 | + $entities[] = $this->mapRowToEntity($row); |
|
302 | + } |
|
303 | + |
|
304 | + $cursor->closeCursor(); |
|
305 | + |
|
306 | + return $entities; |
|
307 | + } |
|
308 | + |
|
309 | + |
|
310 | + /** |
|
311 | + * Returns an db result and throws exceptions when there are more or less |
|
312 | + * results |
|
313 | + * |
|
314 | + * @param IQueryBuilder $query |
|
315 | + * @throws DoesNotExistException if the item does not exist |
|
316 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
317 | + * @return Entity the entity |
|
318 | + * @since 14.0.0 |
|
319 | + */ |
|
320 | + protected function findEntity(IQueryBuilder $query): Entity { |
|
321 | + return $this->mapRowToEntity($this->findOneQuery($query)); |
|
322 | + } |
|
323 | 323 | |
324 | 324 | } |
@@ -52,13 +52,13 @@ discard block |
||
52 | 52 | * mapped to queries without using sql |
53 | 53 | * @since 14.0.0 |
54 | 54 | */ |
55 | - public function __construct(IDBConnection $db, string $tableName, string $entityClass=null){ |
|
55 | + public function __construct(IDBConnection $db, string $tableName, string $entityClass = null) { |
|
56 | 56 | $this->db = $db; |
57 | 57 | $this->tableName = $tableName; |
58 | 58 | |
59 | 59 | // if not given set the entity name to the class without the mapper part |
60 | 60 | // cache it here for later use since reflection is slow |
61 | - if($entityClass === null) { |
|
61 | + if ($entityClass === null) { |
|
62 | 62 | $this->entityClass = str_replace('Mapper', '', \get_class($this)); |
63 | 63 | } else { |
64 | 64 | $this->entityClass = $entityClass; |
@@ -109,9 +109,9 @@ discard block |
||
109 | 109 | $qb->insert($this->tableName); |
110 | 110 | |
111 | 111 | // build the fields |
112 | - foreach($properties as $property => $updated) { |
|
112 | + foreach ($properties as $property => $updated) { |
|
113 | 113 | $column = $entity->propertyToColumn($property); |
114 | - $getter = 'get' . ucfirst($property); |
|
114 | + $getter = 'get'.ucfirst($property); |
|
115 | 115 | $value = $entity->$getter(); |
116 | 116 | |
117 | 117 | $type = $this->getParameterTypeForProperty($entity, $property); |
@@ -120,8 +120,8 @@ discard block |
||
120 | 120 | |
121 | 121 | $qb->execute(); |
122 | 122 | |
123 | - if($entity->id === null) { |
|
124 | - $entity->setId((int)$qb->getLastInsertId()); |
|
123 | + if ($entity->id === null) { |
|
124 | + $entity->setId((int) $qb->getLastInsertId()); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | return $entity; |
@@ -157,13 +157,13 @@ discard block |
||
157 | 157 | public function update(Entity $entity): Entity { |
158 | 158 | // if entity wasn't changed it makes no sense to run a db query |
159 | 159 | $properties = $entity->getUpdatedFields(); |
160 | - if(\count($properties) === 0) { |
|
160 | + if (\count($properties) === 0) { |
|
161 | 161 | return $entity; |
162 | 162 | } |
163 | 163 | |
164 | 164 | // entity needs an id |
165 | 165 | $id = $entity->getId(); |
166 | - if($id === null){ |
|
166 | + if ($id === null) { |
|
167 | 167 | throw new \InvalidArgumentException( |
168 | 168 | 'Entity which should be updated has no id'); |
169 | 169 | } |
@@ -177,9 +177,9 @@ discard block |
||
177 | 177 | $qb->update($this->tableName); |
178 | 178 | |
179 | 179 | // build the fields |
180 | - foreach($properties as $property => $updated) { |
|
180 | + foreach ($properties as $property => $updated) { |
|
181 | 181 | $column = $entity->propertyToColumn($property); |
182 | - $getter = 'get' . ucfirst($property); |
|
182 | + $getter = 'get'.ucfirst($property); |
|
183 | 183 | $value = $entity->$getter(); |
184 | 184 | |
185 | 185 | $type = $this->getParameterTypeForProperty($entity, $property); |
@@ -206,11 +206,11 @@ discard block |
||
206 | 206 | protected function getParameterTypeForProperty(Entity $entity, string $property): int { |
207 | 207 | $types = $entity->getFieldTypes(); |
208 | 208 | |
209 | - if(!isset($types[ $property ])) { |
|
209 | + if (!isset($types[$property])) { |
|
210 | 210 | return IQueryBuilder::PARAM_STR; |
211 | 211 | } |
212 | 212 | |
213 | - switch($types[ $property ]) { |
|
213 | + switch ($types[$property]) { |
|
214 | 214 | case 'int': |
215 | 215 | case 'integer': |
216 | 216 | return IQueryBuilder::PARAM_INT; |
@@ -240,7 +240,7 @@ discard block |
||
240 | 240 | $cursor = $query->execute(); |
241 | 241 | |
242 | 242 | $row = $cursor->fetch(); |
243 | - if($row === false) { |
|
243 | + if ($row === false) { |
|
244 | 244 | $cursor->closeCursor(); |
245 | 245 | $msg = $this->buildDebugMessage( |
246 | 246 | 'Did expect one result but found none when executing', $query |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | |
251 | 251 | $row2 = $cursor->fetch(); |
252 | 252 | $cursor->closeCursor(); |
253 | - if($row2 !== false ) { |
|
253 | + if ($row2 !== false) { |
|
254 | 254 | $msg = $this->buildDebugMessage( |
255 | 255 | 'Did not expect more than one result when executing', $query |
256 | 256 | ); |
@@ -267,8 +267,8 @@ discard block |
||
267 | 267 | * @since 14.0.0 |
268 | 268 | */ |
269 | 269 | private function buildDebugMessage(string $msg, IQueryBuilder $sql): string { |
270 | - return $msg . |
|
271 | - ': query "' . $sql->getSQL() . '"; '; |
|
270 | + return $msg. |
|
271 | + ': query "'.$sql->getSQL().'"; '; |
|
272 | 272 | } |
273 | 273 | |
274 | 274 | |
@@ -281,7 +281,7 @@ discard block |
||
281 | 281 | * @since 14.0.0 |
282 | 282 | */ |
283 | 283 | protected function mapRowToEntity(array $row): Entity { |
284 | - return \call_user_func($this->entityClass .'::fromRow', $row); |
|
284 | + return \call_user_func($this->entityClass.'::fromRow', $row); |
|
285 | 285 | } |
286 | 286 | |
287 | 287 | |
@@ -297,7 +297,7 @@ discard block |
||
297 | 297 | |
298 | 298 | $entities = []; |
299 | 299 | |
300 | - while($row = $cursor->fetch()){ |
|
300 | + while ($row = $cursor->fetch()) { |
|
301 | 301 | $entities[] = $this->mapRowToEntity($row); |
302 | 302 | } |
303 | 303 |