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