Complex classes like MySQLData often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MySQLData, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class MySQLData extends AbstractData |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Holds the Doctrine DBAL instance. |
||
| 26 | * @var Connection |
||
| 27 | */ |
||
| 28 | protected $database; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Flag whether to use UUIDs as primary key. |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | protected $useUUIDs; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Adds the soft deletion parameters if activated. |
||
| 38 | * |
||
| 39 | * @param EntityDefinition $definition |
||
| 40 | * the entity definition which might have soft deletion activated |
||
| 41 | * @param QueryBuilder $queryBuilder |
||
| 42 | * the query builder to add the deletion condition to |
||
| 43 | * @param string $fieldPrefix |
||
| 44 | * the prefix to add before the deleted_at field like an table alias |
||
| 45 | * @param string $method |
||
| 46 | * the method to use of the query builder, "where" or "andWhere" |
||
| 47 | */ |
||
| 48 | 35 | protected function addSoftDeletionToQuery(EntityDefinition $definition, QueryBuilder $queryBuilder, $fieldPrefix = '', $method = 'andWhere') |
|
| 49 | { |
||
| 50 | 35 | if (!$definition->isHardDeletion()) { |
|
| 51 | 35 | $queryBuilder->$method($fieldPrefix.'deleted_at IS NULL'); |
|
| 52 | 35 | } |
|
| 53 | 35 | } |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Sets the values and parameters of the upcoming given query according |
||
| 57 | * to the entity. |
||
| 58 | * |
||
| 59 | * @param Entity $entity |
||
| 60 | * the entity with its fields and values |
||
| 61 | * @param QueryBuilder $queryBuilder |
||
| 62 | * the upcoming query |
||
| 63 | * @param string $setMethod |
||
| 64 | * what method to use on the QueryBuilder: 'setValue' or 'set' |
||
| 65 | */ |
||
| 66 | 34 | protected function setValuesAndParameters(Entity $entity, QueryBuilder $queryBuilder, $setMethod) |
|
| 67 | { |
||
| 68 | 34 | $formFields = $this->getFormFields(); |
|
| 69 | 34 | $count = count($formFields); |
|
| 70 | 34 | for ($i = 0; $i < $count; ++$i) { |
|
| 71 | 34 | $type = $this->definition->getType($formFields[$i]); |
|
| 72 | 34 | $value = $entity->get($formFields[$i]); |
|
| 73 | 34 | if ($type == 'boolean') { |
|
| 74 | 34 | $value = $value ? 1 : 0; |
|
| 75 | 34 | } |
|
| 76 | 34 | if ($type == 'reference' && is_array($value)) { |
|
| 77 | 4 | $value = $value['id']; |
|
| 78 | 4 | } |
|
| 79 | 34 | $queryBuilder->$setMethod('`'.$formFields[$i].'`', '?'); |
|
| 80 | 34 | $queryBuilder->setParameter($i, $value); |
|
| 81 | 34 | } |
|
| 82 | 34 | } |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Checks whether the by id given entity still has children referencing it. |
||
| 86 | * |
||
| 87 | * @param integer $id |
||
| 88 | * the current entities id |
||
| 89 | * |
||
| 90 | * @return boolean |
||
| 91 | * true if the entity still has children |
||
| 92 | */ |
||
| 93 | 2 | protected function hasChildren($id) |
|
| 94 | { |
||
| 95 | 2 | foreach ($this->definition->getChildren() as $child) { |
|
| 96 | 2 | $queryBuilder = $this->database->createQueryBuilder(); |
|
| 97 | $queryBuilder |
||
| 98 | 2 | ->select('COUNT(id)') |
|
| 99 | 2 | ->from('`'.$child[0].'`', '`'.$child[0].'`') |
|
| 100 | 2 | ->where('`'.$child[1].'` = ?') |
|
| 101 | 2 | ->setParameter(0, $id) |
|
| 102 | ; |
||
| 103 | 2 | $this->addSoftDeletionToQuery($this->getDefinition()->getServiceProvider()->getData($child[2])->getDefinition(), $queryBuilder); |
|
| 104 | 2 | $queryResult = $queryBuilder->execute(); |
|
| 105 | 2 | $result = $queryResult->fetch(\PDO::FETCH_NUM); |
|
| 106 | 2 | if ($result[0] > 0) { |
|
| 107 | 2 | return true; |
|
| 108 | } |
||
| 109 | 2 | } |
|
| 110 | 2 | return false; |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Deletes any many to many references pointing to the given entity. |
||
| 115 | * |
||
| 116 | * @param Entity $entity |
||
| 117 | * the referenced entity |
||
| 118 | */ |
||
| 119 | 4 | protected function deleteManyToManyReferences(Entity $entity) |
|
| 120 | { |
||
| 121 | 4 | foreach ($this->definition->getServiceProvider()->getEntities() as $entityName) { |
|
| 122 | 4 | $data = $this->definition->getServiceProvider()->getData($entityName); |
|
| 123 | 4 | foreach ($data->getDefinition()->getFieldNames(true) as $field) { |
|
| 124 | 4 | if ($data->getDefinition()->getType($field) == 'many') { |
|
| 125 | 4 | $otherEntity = $data->getDefinition()->getSubTypeField($field, 'many', 'entity'); |
|
| 126 | 4 | $otherData = $this->definition->getServiceProvider()->getData($otherEntity); |
|
| 127 | 4 | if ($entity->getDefinition()->getTable() == $otherData->getDefinition()->getTable()) { |
|
| 128 | 3 | $thatField = $data->getDefinition()->getSubTypeField($field, 'many', 'thatField'); |
|
| 129 | 3 | $queryBuilder = $this->database->createQueryBuilder(); |
|
| 130 | $queryBuilder |
||
| 131 | 3 | ->delete('`'.$field.'`') |
|
| 132 | 3 | ->where('`'.$thatField.'` = ?') |
|
| 133 | 3 | ->setParameter(0, $entity->get('id')) |
|
| 134 | 3 | ->execute() |
|
| 135 | ; |
||
| 136 | 3 | } |
|
| 137 | 4 | } |
|
| 138 | 4 | } |
|
| 139 | 4 | } |
|
| 140 | 4 | } |
|
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | 4 | protected function doDelete(Entity $entity, $deleteCascade) |
|
| 146 | { |
||
| 147 | 4 | $id = $entity->get('id'); |
|
| 148 | 4 | if ($deleteCascade) { |
|
| 149 | 3 | $result = $this->deleteChildren($id, $deleteCascade); |
|
| 150 | 3 | if ($result !== static::DELETION_SUCCESS) { |
|
| 151 | 1 | return $result; |
|
| 152 | } |
||
| 153 | 4 | } elseif ($this->hasChildren($id)) { |
|
| 154 | 2 | return static::DELETION_FAILED_STILL_REFERENCED; |
|
| 155 | } |
||
| 156 | |||
| 157 | 4 | $this->deleteManyToManyReferences($entity); |
|
| 158 | |||
| 159 | 4 | $query = $this->database->createQueryBuilder(); |
|
| 160 | 4 | if ($this->definition->isHardDeletion()) { |
|
| 161 | 3 | $query->delete('`'.$this->definition->getTable().'`'); |
|
| 162 | 3 | } else { |
|
| 163 | $query |
||
| 164 | 3 | ->update('`'.$this->definition->getTable().'`') |
|
| 165 | 3 | ->set('deleted_at', 'UTC_TIMESTAMP()') |
|
| 166 | ; |
||
| 167 | } |
||
| 168 | $query |
||
| 169 | 4 | ->where('id = ?') |
|
| 170 | 4 | ->setParameter(0, $id) |
|
| 171 | 4 | ->execute() |
|
| 172 | ; |
||
| 173 | 4 | return static::DELETION_SUCCESS; |
|
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Gets all possible many-to-many ids existing for this definition. |
||
| 178 | * |
||
| 179 | * @param array $fields |
||
| 180 | * the many field names to fetch for |
||
| 181 | * @param $params |
||
| 182 | * the parameters the possible many field values to fetch for |
||
| 183 | * @return array |
||
| 184 | * an array of this many-to-many ids |
||
| 185 | */ |
||
| 186 | 35 | protected function getManyIds(array $fields, array $params) |
|
| 187 | { |
||
| 188 | 35 | $manyIds = []; |
|
| 189 | 35 | foreach ($fields as $field) { |
|
| 190 | 3 | $thisField = $this->definition->getSubTypeField($field, 'many', 'thisField'); |
|
| 191 | 3 | $thatField = $this->definition->getSubTypeField($field, 'many', 'thatField'); |
|
| 192 | 3 | $queryBuilder = $this->database->createQueryBuilder(); |
|
| 193 | $queryBuilder |
||
| 194 | 3 | ->select('`'.$thisField.'`') |
|
| 195 | 3 | ->from($field) |
|
| 196 | 3 | ->where('`'.$thatField.'` IN (?)') |
|
| 197 | 3 | ->setParameter(0, array_column($params[$field], 'id'), Connection::PARAM_STR_ARRAY) |
|
| 198 | 3 | ->groupBy('`'.$thisField.'`') |
|
| 199 | ; |
||
| 200 | 3 | $queryResult = $queryBuilder->execute(); |
|
| 201 | 3 | $manyResults = $queryResult->fetchAll(\PDO::FETCH_ASSOC); |
|
| 202 | 3 | $manyIds = array_merge($manyIds, array_column($manyResults, $thisField)); |
|
| 203 | |||
| 204 | 35 | } |
|
| 205 | 35 | return $manyIds; |
|
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Adds sorting parameters to the query. |
||
| 210 | * |
||
| 211 | * @param QueryBuilder $queryBuilder |
||
| 212 | * the query |
||
| 213 | * @param $filter |
||
| 214 | * the filter all resulting entities must fulfill, the keys as field names |
||
| 215 | * @param $filterOperators |
||
| 216 | * the operators of the filter like "=" defining the full condition of the field |
||
| 217 | */ |
||
| 218 | 35 | protected function addFilter(QueryBuilder $queryBuilder, array $filter, array $filterOperators) |
|
| 219 | { |
||
| 220 | 35 | $i = 0; |
|
| 221 | 35 | $manyFields = []; |
|
| 222 | 35 | foreach ($filter as $field => $value) { |
|
| 223 | 34 | if ($this->definition->getType($field) === 'many') { |
|
| 224 | 2 | $manyFields[] = $field; |
|
| 225 | 2 | continue; |
|
| 226 | } |
||
| 227 | 34 | if ($value === null) { |
|
| 228 | 1 | $queryBuilder->andWhere('`'.$field.'` IS NULL'); |
|
| 229 | 1 | } else { |
|
| 230 | 34 | $operator = array_key_exists($field, $filterOperators) ? $filterOperators[$field] : '='; |
|
| 231 | $queryBuilder |
||
| 232 | 34 | ->andWhere('`'.$field.'` '.$operator.' ?') |
|
| 233 | 34 | ->setParameter($i, $value, \PDO::PARAM_STR); |
|
| 234 | } |
||
| 235 | 34 | $i++; |
|
| 236 | 35 | } |
|
| 237 | 35 | $idsToInclude = $this->getManyIds($manyFields, $filter); |
|
| 238 | 35 | if (!empty($idsToInclude)) { |
|
| 239 | $queryBuilder |
||
| 240 | 2 | ->andWhere('id IN (?)') |
|
| 241 | 2 | ->setParameter($i, $idsToInclude, Connection::PARAM_STR_ARRAY) |
|
| 242 | ; |
||
| 243 | 2 | } |
|
| 244 | 35 | } |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Adds pagination parameters to the query. |
||
| 248 | * |
||
| 249 | * @param QueryBuilder $queryBuilder |
||
| 250 | * the query |
||
| 251 | * @param integer|null $skip |
||
| 252 | * the rows to skip |
||
| 253 | * @param integer|null $amount |
||
| 254 | * the maximum amount of rows |
||
| 255 | */ |
||
| 256 | 35 | protected function addPagination(QueryBuilder $queryBuilder, $skip, $amount) |
|
| 257 | { |
||
| 258 | 35 | $queryBuilder->setMaxResults(9999999999); |
|
| 259 | 35 | if ($amount !== null) { |
|
| 260 | 5 | $queryBuilder->setMaxResults(abs(intval($amount))); |
|
| 261 | 5 | } |
|
| 262 | 35 | if ($skip !== null) { |
|
| 263 | 5 | $queryBuilder->setFirstResult(abs(intval($skip))); |
|
| 264 | 5 | } |
|
| 265 | 35 | } |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Adds sorting parameters to the query. |
||
| 269 | * |
||
| 270 | * @param QueryBuilder $queryBuilder |
||
| 271 | * the query |
||
| 272 | * @param string|null $sortField |
||
| 273 | * the sort field |
||
| 274 | * @param boolean|null $sortAscending |
||
| 275 | * true if sort ascending, false if descending |
||
| 276 | */ |
||
| 277 | 35 | protected function addSort(QueryBuilder $queryBuilder, $sortField, $sortAscending) |
|
| 278 | { |
||
| 279 | 35 | if ($sortField !== null) { |
|
| 280 | |||
| 281 | 5 | $type = $this->definition->getType($sortField); |
|
| 282 | 5 | if ($type === 'many') { |
|
| 283 | 1 | $sortField = $this->definition->getInitialSortField(); |
|
| 284 | 1 | } |
|
| 285 | |||
| 286 | 5 | $order = $sortAscending === true ? 'ASC' : 'DESC'; |
|
| 287 | 5 | $queryBuilder->orderBy('`'.$sortField.'`', $order); |
|
| 288 | 5 | } |
|
| 289 | 35 | } |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Adds the id and name of referenced entities to the given entities. The |
||
| 293 | * reference field is before the raw id of the referenced entity and after |
||
| 294 | * the fetch, it's an array with the keys id and name. |
||
| 295 | * |
||
| 296 | * @param Entity[] &$entities |
||
| 297 | * the entities to fetch the references for |
||
| 298 | * @param string $field |
||
| 299 | * the reference field |
||
| 300 | */ |
||
| 301 | 21 | protected function fetchReferencesForField(array &$entities, $field) |
|
| 302 | { |
||
| 303 | 21 | $nameField = $this->definition->getSubTypeField($field, 'reference', 'nameField'); |
|
| 304 | 21 | $queryBuilder = $this->database->createQueryBuilder(); |
|
| 305 | |||
| 306 | 21 | $ids = $this->getReferenceIds($entities, $field); |
|
| 307 | |||
| 308 | 21 | $referenceEntity = $this->definition->getSubTypeField($field, 'reference', 'entity'); |
|
| 309 | 21 | $table = $this->definition->getServiceProvider()->getData($referenceEntity)->getDefinition()->getTable(); |
|
| 310 | $queryBuilder |
||
| 311 | 21 | ->from('`'.$table.'`', '`'.$table.'`') |
|
| 312 | 21 | ->where('id IN (?)') |
|
| 313 | ; |
||
| 314 | 21 | $this->addSoftDeletionToQuery($this->definition, $queryBuilder); |
|
| 315 | 21 | if ($nameField) { |
|
| 316 | 21 | $queryBuilder->select('id', $nameField); |
|
| 317 | 21 | } else { |
|
| 318 | 21 | $queryBuilder->select('id'); |
|
| 319 | } |
||
| 320 | |||
| 321 | 21 | $queryBuilder->setParameter(0, $ids, Connection::PARAM_STR_ARRAY); |
|
| 322 | |||
| 323 | 21 | $queryResult = $queryBuilder->execute(); |
|
| 324 | 21 | $rows = $queryResult->fetchAll(\PDO::FETCH_ASSOC); |
|
| 325 | 21 | $amount = count($entities); |
|
| 326 | 21 | foreach ($rows as $row) { |
|
| 327 | 21 | for ($i = 0; $i < $amount; ++$i) { |
|
| 328 | 21 | if ($entities[$i]->get($field) == $row['id']) { |
|
| 329 | 21 | $value = ['id' => $entities[$i]->get($field)]; |
|
| 330 | 21 | if ($nameField) { |
|
| 331 | 21 | $value['name'] = $row[$nameField]; |
|
| 332 | 21 | } |
|
| 333 | 21 | $entities[$i]->set($field, $value); |
|
| 334 | 21 | } |
|
| 335 | 21 | } |
|
| 336 | 21 | } |
|
| 337 | 21 | } |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Generates a new UUID. |
||
| 341 | * |
||
| 342 | * @return string|null |
||
| 343 | * the new UUID or null if this instance isn't configured to do so |
||
| 344 | */ |
||
| 345 | 34 | protected function generateUUID() |
|
| 346 | { |
||
| 347 | 34 | $uuid = null; |
|
| 348 | 34 | if ($this->useUUIDs) { |
|
| 349 | 1 | $sql = 'SELECT UUID() as id'; |
|
| 350 | 1 | $result = $this->database->fetchAssoc($sql); |
|
| 351 | 1 | $uuid = $result['id']; |
|
| 352 | 1 | } |
|
| 353 | 34 | return $uuid; |
|
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Enriches the given mapping of entity id to raw entity data with some many-to-many data. |
||
| 358 | * |
||
| 359 | * @param array $idToData |
||
| 360 | * a reference to the map entity id to raw entity data |
||
| 361 | * @param $manyField |
||
| 362 | * the many field to enrich data with |
||
| 363 | */ |
||
| 364 | 34 | protected function enrichWithManyField(&$idToData, $manyField) |
|
| 365 | { |
||
| 366 | 34 | $queryBuilder = $this->database->createQueryBuilder(); |
|
| 367 | 34 | $nameField = $this->definition->getSubTypeField($manyField, 'many', 'nameField'); |
|
| 368 | 34 | $thisField = $this->definition->getSubTypeField($manyField, 'many', 'thisField'); |
|
| 369 | 34 | $thatField = $this->definition->getSubTypeField($manyField, 'many', 'thatField'); |
|
| 370 | 34 | $entity = $this->definition->getSubTypeField($manyField, 'many', 'entity'); |
|
| 371 | 34 | $entityDefinition = $this->definition->getServiceProvider()->getData($entity)->getDefinition(); |
|
| 372 | 34 | $entityTable = $entityDefinition->getTable(); |
|
| 373 | 34 | $nameSelect = $nameField !== null ? ', t2.`'.$nameField.'` AS name' : ''; |
|
| 374 | $queryBuilder |
||
| 375 | 34 | ->select('t1.`'.$thisField.'` AS this, t1.`'.$thatField.'` AS id'.$nameSelect) |
|
| 376 | 34 | ->from('`'.$manyField.'`', 't1') |
|
| 377 | 34 | ->leftJoin('t1', '`'.$entityTable.'`', 't2', 't2.id = t1.`'.$thatField.'`') |
|
| 378 | 34 | ->where('t1.`'.$thisField.'` IN (?)') |
|
| 379 | ; |
||
| 380 | 34 | $this->addSoftDeletionToQuery($entityDefinition, $queryBuilder); |
|
| 381 | 34 | $queryBuilder->setParameter(0, array_keys($idToData), Connection::PARAM_STR_ARRAY); |
|
| 382 | 34 | $queryResult = $queryBuilder->execute(); |
|
| 383 | 34 | $manyReferences = $queryResult->fetchAll(\PDO::FETCH_ASSOC); |
|
| 384 | 34 | foreach ($manyReferences as $manyReference) { |
|
| 385 | 2 | $entityId = $manyReference['this']; |
|
| 386 | 2 | unset($manyReference['this']); |
|
| 387 | 2 | $idToData[$entityId][$manyField][] = $manyReference; |
|
| 388 | 34 | } |
|
| 389 | 34 | } |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Fetches to the rows belonging many-to-many entries and adds them to the rows. |
||
| 393 | * |
||
| 394 | * @param array $rows |
||
| 395 | * the rows to enrich |
||
| 396 | * @return array |
||
| 397 | * the enriched rows |
||
| 398 | */ |
||
| 399 | 35 | protected function enrichWithMany(array $rows) |
|
| 400 | { |
||
| 401 | 35 | $manyFields = $this->getManyFields(); |
|
| 402 | 35 | $idToData = []; |
|
| 403 | 35 | foreach ($rows as $row) { |
|
| 404 | 34 | foreach ($manyFields as $manyField) { |
|
| 405 | 34 | $row[$manyField] = []; |
|
| 406 | 34 | } |
|
| 407 | 34 | $idToData[$row['id']] = $row; |
|
| 408 | 35 | } |
|
| 409 | 35 | foreach ($manyFields as $manyField) { |
|
| 410 | 34 | $this->enrichWithManyField($idToData, $manyField); |
|
| 411 | 35 | } |
|
| 412 | 35 | return array_values($idToData); |
|
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * First, deletes all to the given entity related many-to-many entries from the DB |
||
| 417 | * and then writes them again. |
||
| 418 | * |
||
| 419 | * @param Entity $entity |
||
| 420 | * the entity to save the many-to-many entries of |
||
| 421 | */ |
||
| 422 | 34 | protected function saveMany(Entity $entity) |
|
| 423 | { |
||
| 424 | 34 | $manyFields = $this->getManyFields(); |
|
| 425 | 34 | $id = $entity->get('id'); |
|
| 426 | 34 | foreach ($manyFields as $manyField) { |
|
| 427 | 34 | $thisField = '`'.$this->definition->getSubTypeField($manyField, 'many', 'thisField').'`'; |
|
| 428 | 34 | $thatField = '`'.$this->definition->getSubTypeField($manyField, 'many', 'thatField').'`'; |
|
| 429 | 34 | $this->database->delete($manyField, [$thisField => $id]); |
|
| 430 | 34 | $manyValues = $entity->get($manyField) ?: []; |
|
| 431 | 34 | foreach ($manyValues as $thatId) { |
|
| 432 | 8 | $this->database->insert($manyField, [ |
|
| 433 | 8 | $thisField => $id, |
|
| 434 | 8 | $thatField => $thatId['id'] |
|
| 435 | 8 | ]); |
|
| 436 | 34 | } |
|
| 437 | 34 | } |
|
| 438 | 34 | } |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Adds the id and name of referenced entities to the given entities. Each |
||
| 442 | * reference field is before the raw id of the referenced entity and after |
||
| 443 | * the fetch, it's an array with the keys id and name. |
||
| 444 | * |
||
| 445 | * @param Entity[] &$entities |
||
| 446 | * the entities to fetch the references for |
||
| 447 | * |
||
| 448 | * @return void |
||
| 449 | */ |
||
| 450 | 35 | protected function enrichWithReference(array &$entities) |
|
| 451 | { |
||
| 452 | 35 | if (empty($entities)) { |
|
| 453 | 11 | return; |
|
| 454 | } |
||
| 455 | 34 | foreach ($this->definition->getFieldNames() as $field) { |
|
| 456 | 34 | if ($this->definition->getType($field) !== 'reference') { |
|
| 457 | 34 | continue; |
|
| 458 | } |
||
| 459 | 21 | $this->fetchReferencesForField($entities, $field); |
|
| 460 | 34 | } |
|
| 461 | 34 | } |
|
| 462 | |||
| 463 | /** |
||
| 464 | * {@inheritdoc} |
||
| 465 | */ |
||
| 466 | 34 | protected function doCreate(Entity $entity) |
|
| 467 | { |
||
| 468 | |||
| 469 | 34 | $queryBuilder = $this->database->createQueryBuilder(); |
|
| 470 | $queryBuilder |
||
| 471 | 34 | ->insert('`'.$this->definition->getTable().'`') |
|
| 472 | 34 | ->setValue('created_at', 'UTC_TIMESTAMP()') |
|
| 473 | 34 | ->setValue('updated_at', 'UTC_TIMESTAMP()'); |
|
| 474 | 34 | if ($this->definition->hasOptimisticLocking()) { |
|
| 475 | 21 | $queryBuilder->setValue('version', 0); |
|
| 476 | 21 | } |
|
| 477 | |||
| 478 | 34 | $this->setValuesAndParameters($entity, $queryBuilder, 'setValue'); |
|
| 479 | |||
| 480 | 34 | $id = $this->generateUUID(); |
|
| 481 | 34 | if ($this->useUUIDs) { |
|
| 482 | 1 | $queryBuilder->setValue('`id`', '?'); |
|
| 483 | 1 | $uuidI = count($this->getFormFields()); |
|
| 484 | 1 | $queryBuilder->setParameter($uuidI, $id); |
|
| 485 | 1 | } |
|
| 486 | |||
| 487 | 34 | $queryBuilder->execute(); |
|
| 488 | |||
| 489 | 34 | if (!$this->useUUIDs) { |
|
| 490 | 33 | $id = $this->database->lastInsertId(); |
|
| 491 | 33 | } |
|
| 492 | |||
| 493 | 34 | $this->enrichEntityWithMetaData($id, $entity); |
|
| 494 | 34 | $this->saveMany($entity); |
|
| 495 | 34 | $entities = [$entity]; |
|
| 496 | 34 | $this->enrichWithReference($entities); |
|
| 497 | |||
| 498 | 34 | return true; |
|
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * {@inheritdoc} |
||
| 503 | */ |
||
| 504 | 13 | protected function doUpdate(Entity $entity) |
|
| 505 | { |
||
| 506 | 13 | $queryBuilder = $this->database->createQueryBuilder(); |
|
| 507 | 13 | $queryBuilder->update('`'.$this->definition->getTable().'`') |
|
| 508 | 13 | ->set('updated_at', 'UTC_TIMESTAMP()') |
|
| 509 | 13 | ->where('id = ?') |
|
| 510 | 13 | ->setParameter(count($this->getFormFields()), $entity->get('id')); |
|
| 511 | 13 | if ($this->definition->hasOptimisticLocking()) { |
|
| 512 | 2 | $queryBuilder->set('version', 'version + 1'); |
|
| 513 | 2 | } |
|
| 514 | |||
| 515 | 13 | $this->setValuesAndParameters($entity, $queryBuilder, 'set'); |
|
| 516 | 13 | $affected = $queryBuilder->execute(); |
|
| 517 | |||
| 518 | 13 | $this->saveMany($entity); |
|
| 519 | 13 | $entities = [$entity]; |
|
| 520 | 13 | $this->enrichWithReference($entities); |
|
| 521 | 13 | return $affected > 0; |
|
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Constructor. |
||
| 526 | * |
||
| 527 | * @param EntityDefinition $definition |
||
| 528 | * the entity definition |
||
| 529 | * @param FilesystemInterface $filesystem |
||
| 530 | * the filesystem to use |
||
| 531 | * @param Connection $database |
||
| 532 | * the Doctrine DBAL instance to use |
||
| 533 | * @param boolean $useUUIDs |
||
| 534 | * flag whether to use UUIDs as primary key |
||
| 535 | */ |
||
| 536 | 78 | public function __construct(EntityDefinition $definition, FilesystemInterface $filesystem, Connection $database, $useUUIDs) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * {@inheritdoc} |
||
| 547 | */ |
||
| 548 | 34 | public function get($id) |
|
| 556 | |||
| 557 | /** |
||
| 558 | * {@inheritdoc} |
||
| 559 | */ |
||
| 560 | 35 | public function listEntries(array $filter = [], array $filterOperators = [], $skip = null, $amount = null, $sortField = null, $sortAscending = null) |
|
| 561 | { |
||
| 562 | 35 | $fieldNames = $this->definition->getFieldNames(); |
|
| 563 | |||
| 564 | 35 | $queryBuilder = $this->database->createQueryBuilder(); |
|
| 565 | 35 | $table = $this->definition->getTable(); |
|
| 566 | $queryBuilder |
||
| 567 | 35 | ->select('`'.implode('`,`', $fieldNames).'`') |
|
| 568 | 35 | ->from('`'.$table.'`', '`'.$table.'`') |
|
| 569 | ; |
||
| 570 | |||
| 571 | 35 | $this->addFilter($queryBuilder, $filter, $filterOperators); |
|
| 572 | 35 | $this->addSoftDeletionToQuery($this->definition, $queryBuilder); |
|
| 573 | 35 | $this->addPagination($queryBuilder, $skip, $amount); |
|
| 574 | 35 | $this->addSort($queryBuilder, $sortField, $sortAscending); |
|
| 575 | |||
| 576 | 35 | $queryResult = $queryBuilder->execute(); |
|
| 577 | 35 | $rows = $queryResult->fetchAll(\PDO::FETCH_ASSOC); |
|
| 578 | 35 | $rows = $this->enrichWithMany($rows); |
|
| 579 | 35 | $entities = []; |
|
| 580 | 35 | foreach ($rows as $row) { |
|
| 581 | 34 | $entities[] = $this->hydrate($row); |
|
| 582 | 35 | } |
|
| 583 | 35 | $this->enrichWithReference($entities); |
|
| 584 | 35 | return $entities; |
|
| 585 | } |
||
| 586 | |||
| 587 | /** |
||
| 588 | * {@inheritdoc} |
||
| 589 | */ |
||
| 590 | 8 | public function getIdToNameMap($entity, $nameField) |
|
| 612 | |||
| 613 | /** |
||
| 614 | * {@inheritdoc} |
||
| 615 | */ |
||
| 616 | 12 | public function countBy($table, array $params, array $paramsOperators, $excludeDeleted) |
|
| 617 | { |
||
| 618 | 12 | $queryBuilder = $this->database->createQueryBuilder(); |
|
| 619 | $queryBuilder |
||
| 620 | 12 | ->select('COUNT(id)') |
|
| 621 | 12 | ->from('`'.$table.'`', '`'.$table.'`') |
|
| 622 | ; |
||
| 623 | |||
| 624 | 12 | $deletedExcluder = 'where'; |
|
| 625 | 12 | $i = 0; |
|
| 626 | 12 | $manyFields = []; |
|
| 627 | 12 | foreach ($params as $name => $value) { |
|
| 628 | 9 | if ($this->definition->getType($name) === 'many') { |
|
| 629 | 2 | $manyFields[] = $name; |
|
| 630 | 2 | continue; |
|
| 657 | |||
| 658 | /** |
||
| 659 | * {@inheritdoc} |
||
| 660 | */ |
||
| 661 | 2 | public function hasManySet($field, array $thatIds, $excludeId = null) |
|
| 690 | |||
| 691 | } |
||
| 692 |