Complex classes like ModelManager 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 ModelManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class ModelManager implements ModelManagerInterface, LockInterface |
||
| 43 | { |
||
| 44 | public const ID_SEPARATOR = '~'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var ManagerRegistry |
||
| 48 | */ |
||
| 49 | protected $registry; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var PropertyAccessorInterface |
||
| 53 | */ |
||
| 54 | protected $propertyAccessor; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var EntityManager[] |
||
| 58 | */ |
||
| 59 | protected $cache = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * NEXT_MAJOR: Make $propertyAccessor mandatory. |
||
| 63 | */ |
||
| 64 | public function __construct(ManagerRegistry $registry, ?PropertyAccessorInterface $propertyAccessor = null) |
||
| 65 | { |
||
| 66 | $this->registry = $registry; |
||
| 67 | |||
| 68 | // NEXT_MAJOR: Remove this block. |
||
| 69 | if (null === $propertyAccessor) { |
||
| 70 | @trigger_error(sprintf( |
||
|
|
|||
| 71 | 'Constructing "%s" without passing an instance of "%s" as second argument is deprecated since' |
||
| 72 | .' sonata-project/doctrine-orm-admin-bundle 3.x and will be mandatory in 4.0', |
||
| 73 | __CLASS__, |
||
| 74 | PropertyAccessorInterface::class |
||
| 75 | ), E_USER_DEPRECATED); |
||
| 76 | |||
| 77 | $propertyAccessor = PropertyAccess::createPropertyAccessor(); |
||
| 78 | } |
||
| 79 | |||
| 80 | $this->propertyAccessor = $propertyAccessor; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param string $class |
||
| 85 | * |
||
| 86 | * @return ClassMetadata |
||
| 87 | */ |
||
| 88 | public function getMetadata($class) |
||
| 89 | { |
||
| 90 | return $this->getEntityManager($class)->getMetadataFactory()->getMetadataFor($class); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns the model's metadata holding the fully qualified property, and the last |
||
| 95 | * property name. |
||
| 96 | * |
||
| 97 | * @param string $baseClass The base class of the model holding the fully qualified property |
||
| 98 | * @param string $propertyFullName The name of the fully qualified property (dot ('.') separated |
||
| 99 | * property string) |
||
| 100 | * |
||
| 101 | * @return array( |
||
| 102 | * \Doctrine\ORM\Mapping\ClassMetadata $parentMetadata, |
||
| 103 | * string $lastPropertyName, |
||
| 104 | * array $parentAssociationMappings |
||
| 105 | * ) |
||
| 106 | */ |
||
| 107 | public function getParentMetadataForProperty($baseClass, $propertyFullName) |
||
| 108 | { |
||
| 109 | $nameElements = explode('.', $propertyFullName); |
||
| 110 | $lastPropertyName = array_pop($nameElements); |
||
| 111 | $class = $baseClass; |
||
| 112 | $parentAssociationMappings = []; |
||
| 113 | |||
| 114 | foreach ($nameElements as $nameElement) { |
||
| 115 | $metadata = $this->getMetadata($class); |
||
| 116 | |||
| 117 | if (isset($metadata->associationMappings[$nameElement])) { |
||
| 118 | $parentAssociationMappings[] = $metadata->associationMappings[$nameElement]; |
||
| 119 | $class = $metadata->getAssociationTargetClass($nameElement); |
||
| 120 | |||
| 121 | continue; |
||
| 122 | } |
||
| 123 | |||
| 124 | break; |
||
| 125 | } |
||
| 126 | |||
| 127 | $properties = \array_slice($nameElements, \count($parentAssociationMappings)); |
||
| 128 | $properties[] = $lastPropertyName; |
||
| 129 | |||
| 130 | return [$this->getMetadata($class), implode('.', $properties), $parentAssociationMappings]; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $class |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | public function hasMetadata($class) |
||
| 139 | { |
||
| 140 | return $this->getEntityManager($class)->getMetadataFactory()->hasMetadataFor($class); |
||
| 141 | } |
||
| 142 | |||
| 143 | public function getNewFieldDescriptionInstance($class, $name, array $options = []) |
||
| 144 | { |
||
| 145 | if (!\is_string($name)) { |
||
| 146 | throw new \RuntimeException('The name argument must be a string'); |
||
| 147 | } |
||
| 148 | |||
| 149 | if (!isset($options['route']['name'])) { |
||
| 150 | $options['route']['name'] = 'edit'; |
||
| 151 | } |
||
| 152 | |||
| 153 | if (!isset($options['route']['parameters'])) { |
||
| 154 | $options['route']['parameters'] = []; |
||
| 155 | } |
||
| 156 | |||
| 157 | list($metadata, $propertyName, $parentAssociationMappings) = $this->getParentMetadataForProperty($class, $name); |
||
| 158 | |||
| 159 | $fieldDescription = new FieldDescription(); |
||
| 160 | $fieldDescription->setName($name); |
||
| 161 | $fieldDescription->setOptions($options); |
||
| 162 | $fieldDescription->setParentAssociationMappings($parentAssociationMappings); |
||
| 163 | |||
| 164 | if (isset($metadata->associationMappings[$propertyName])) { |
||
| 165 | $fieldDescription->setAssociationMapping($metadata->associationMappings[$propertyName]); |
||
| 166 | } |
||
| 167 | |||
| 168 | if (isset($metadata->fieldMappings[$propertyName])) { |
||
| 169 | $fieldDescription->setFieldMapping($metadata->fieldMappings[$propertyName]); |
||
| 170 | } |
||
| 171 | |||
| 172 | return $fieldDescription; |
||
| 173 | } |
||
| 174 | |||
| 175 | public function create($object) |
||
| 176 | { |
||
| 177 | try { |
||
| 178 | $entityManager = $this->getEntityManager($object); |
||
| 179 | $entityManager->persist($object); |
||
| 180 | $entityManager->flush(); |
||
| 181 | } catch (\PDOException $e) { |
||
| 182 | throw new ModelManagerException( |
||
| 183 | sprintf('Failed to create object: %s', ClassUtils::getClass($object)), |
||
| 184 | $e->getCode(), |
||
| 185 | $e |
||
| 186 | ); |
||
| 187 | } catch (DBALException $e) { |
||
| 188 | throw new ModelManagerException( |
||
| 189 | sprintf('Failed to create object: %s', ClassUtils::getClass($object)), |
||
| 190 | $e->getCode(), |
||
| 191 | $e |
||
| 192 | ); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | public function update($object) |
||
| 197 | { |
||
| 198 | try { |
||
| 199 | $entityManager = $this->getEntityManager($object); |
||
| 200 | $entityManager->persist($object); |
||
| 201 | $entityManager->flush(); |
||
| 202 | } catch (\PDOException $e) { |
||
| 203 | throw new ModelManagerException( |
||
| 204 | sprintf('Failed to update object: %s', ClassUtils::getClass($object)), |
||
| 205 | $e->getCode(), |
||
| 206 | $e |
||
| 207 | ); |
||
| 208 | } catch (DBALException $e) { |
||
| 209 | throw new ModelManagerException( |
||
| 210 | sprintf('Failed to update object: %s', ClassUtils::getClass($object)), |
||
| 211 | $e->getCode(), |
||
| 212 | $e |
||
| 213 | ); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | public function delete($object) |
||
| 218 | { |
||
| 219 | try { |
||
| 220 | $entityManager = $this->getEntityManager($object); |
||
| 221 | $entityManager->remove($object); |
||
| 222 | $entityManager->flush(); |
||
| 223 | } catch (\PDOException $e) { |
||
| 224 | throw new ModelManagerException( |
||
| 225 | sprintf('Failed to delete object: %s', ClassUtils::getClass($object)), |
||
| 226 | $e->getCode(), |
||
| 227 | $e |
||
| 228 | ); |
||
| 229 | } catch (DBALException $e) { |
||
| 230 | throw new ModelManagerException( |
||
| 231 | sprintf('Failed to delete object: %s', ClassUtils::getClass($object)), |
||
| 232 | $e->getCode(), |
||
| 233 | $e |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | public function getLockVersion($object) |
||
| 239 | { |
||
| 240 | $metadata = $this->getMetadata(ClassUtils::getClass($object)); |
||
| 241 | |||
| 242 | if (!$metadata->isVersioned) { |
||
| 243 | return null; |
||
| 244 | } |
||
| 245 | |||
| 246 | return $metadata->reflFields[$metadata->versionField]->getValue($object); |
||
| 247 | } |
||
| 248 | |||
| 249 | public function lock($object, $expectedVersion) |
||
| 250 | { |
||
| 251 | $metadata = $this->getMetadata(ClassUtils::getClass($object)); |
||
| 252 | |||
| 253 | if (!$metadata->isVersioned) { |
||
| 254 | return; |
||
| 255 | } |
||
| 256 | |||
| 257 | try { |
||
| 258 | $entityManager = $this->getEntityManager($object); |
||
| 259 | $entityManager->lock($object, LockMode::OPTIMISTIC, $expectedVersion); |
||
| 260 | } catch (OptimisticLockException $e) { |
||
| 261 | throw new LockException($e->getMessage(), $e->getCode(), $e); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | public function find($class, $id) |
||
| 266 | { |
||
| 267 | if (null === $id) { |
||
| 268 | @trigger_error(sprintf( |
||
| 269 | 'Passing null as argument 1 for %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.20 and will be not allowed in version 4.0.', |
||
| 270 | __METHOD__ |
||
| 271 | ), E_USER_DEPRECATED); |
||
| 272 | |||
| 273 | return null; |
||
| 274 | } |
||
| 275 | |||
| 276 | $values = array_combine($this->getIdentifierFieldNames($class), explode(self::ID_SEPARATOR, (string) $id)); |
||
| 277 | |||
| 278 | return $this->getEntityManager($class)->getRepository($class)->find($values); |
||
| 279 | } |
||
| 280 | |||
| 281 | public function findBy($class, array $criteria = []) |
||
| 282 | { |
||
| 283 | return $this->getEntityManager($class)->getRepository($class)->findBy($criteria); |
||
| 284 | } |
||
| 285 | |||
| 286 | public function findOneBy($class, array $criteria = []) |
||
| 287 | { |
||
| 288 | return $this->getEntityManager($class)->getRepository($class)->findOneBy($criteria); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string|object $class |
||
| 293 | * |
||
| 294 | * @return EntityManager |
||
| 295 | */ |
||
| 296 | public function getEntityManager($class) |
||
| 297 | { |
||
| 298 | if (\is_object($class)) { |
||
| 299 | $class = \get_class($class); |
||
| 300 | } |
||
| 301 | |||
| 302 | if (!isset($this->cache[$class])) { |
||
| 303 | $em = $this->registry->getManagerForClass($class); |
||
| 304 | |||
| 305 | if (!$em) { |
||
| 306 | throw new \RuntimeException(sprintf('No entity manager defined for class %s', $class)); |
||
| 307 | } |
||
| 308 | |||
| 309 | $this->cache[$class] = $em; |
||
| 310 | } |
||
| 311 | |||
| 312 | return $this->cache[$class]; |
||
| 313 | } |
||
| 314 | |||
| 315 | public function getParentFieldDescription($parentAssociationMapping, $class) |
||
| 316 | { |
||
| 317 | $fieldName = $parentAssociationMapping['fieldName']; |
||
| 318 | |||
| 319 | $metadata = $this->getMetadata($class); |
||
| 320 | |||
| 321 | $associatingMapping = $metadata->associationMappings[$parentAssociationMapping]; |
||
| 322 | |||
| 323 | $fieldDescription = $this->getNewFieldDescriptionInstance($class, $fieldName); |
||
| 324 | $fieldDescription->setName($parentAssociationMapping); |
||
| 325 | $fieldDescription->setAssociationMapping($associatingMapping); |
||
| 326 | |||
| 327 | return $fieldDescription; |
||
| 328 | } |
||
| 329 | |||
| 330 | public function createQuery($class, $alias = 'o') |
||
| 331 | { |
||
| 332 | $repository = $this->getEntityManager($class)->getRepository($class); |
||
| 333 | |||
| 334 | return new ProxyQuery($repository->createQueryBuilder($alias)); |
||
| 335 | } |
||
| 336 | |||
| 337 | public function executeQuery($query) |
||
| 338 | { |
||
| 339 | if ($query instanceof QueryBuilder) { |
||
| 340 | return $query->getQuery()->execute(); |
||
| 341 | } |
||
| 342 | |||
| 343 | return $query->execute(); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * NEXT_MAJOR: Remove this function. |
||
| 348 | * |
||
| 349 | * @deprecated since sonata-project/doctrine-orm-admin-bundle 3.18. To be removed in 4.0. |
||
| 350 | */ |
||
| 351 | public function getModelIdentifier($class) |
||
| 352 | { |
||
| 353 | return $this->getMetadata($class)->identifier; |
||
| 354 | } |
||
| 355 | |||
| 356 | public function getIdentifierValues($entity) |
||
| 357 | { |
||
| 358 | // Fix code has an impact on performance, so disable it ... |
||
| 359 | //$entityManager = $this->getEntityManager($entity); |
||
| 360 | //if (!$entityManager->getUnitOfWork()->isInIdentityMap($entity)) { |
||
| 361 | // throw new \RuntimeException('Entities passed to the choice field must be managed'); |
||
| 362 | //} |
||
| 363 | |||
| 364 | $class = ClassUtils::getClass($entity); |
||
| 365 | $metadata = $this->getMetadata($class); |
||
| 366 | $platform = $this->getEntityManager($class)->getConnection()->getDatabasePlatform(); |
||
| 367 | |||
| 368 | $identifiers = []; |
||
| 369 | |||
| 370 | foreach ($metadata->getIdentifierValues($entity) as $name => $value) { |
||
| 371 | if (!\is_object($value)) { |
||
| 372 | $identifiers[] = $value; |
||
| 373 | |||
| 374 | continue; |
||
| 375 | } |
||
| 376 | |||
| 377 | $fieldType = $metadata->getTypeOfField($name); |
||
| 378 | $type = $fieldType && Type::hasType($fieldType) ? Type::getType($fieldType) : null; |
||
| 379 | if ($type) { |
||
| 380 | $identifiers[] = $this->getValueFromType($value, $type, $fieldType, $platform); |
||
| 381 | |||
| 382 | continue; |
||
| 383 | } |
||
| 384 | |||
| 385 | $identifierMetadata = $this->getMetadata(ClassUtils::getClass($value)); |
||
| 386 | |||
| 387 | foreach ($identifierMetadata->getIdentifierValues($value) as $value) { |
||
| 388 | $identifiers[] = $value; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | return $identifiers; |
||
| 393 | } |
||
| 394 | |||
| 395 | public function getIdentifierFieldNames($class) |
||
| 396 | { |
||
| 397 | return $this->getMetadata($class)->getIdentifierFieldNames(); |
||
| 398 | } |
||
| 399 | |||
| 400 | public function getNormalizedIdentifier($entity) |
||
| 401 | { |
||
| 402 | // NEXT_MAJOR: Remove the following 2 checks and declare "object" as type for argument 1. |
||
| 403 | if (null === $entity) { |
||
| 404 | @trigger_error(sprintf( |
||
| 405 | 'Passing null as argument 1 for %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.20 and will be not allowed in version 4.0.', |
||
| 406 | __METHOD__ |
||
| 407 | ), E_USER_DEPRECATED); |
||
| 408 | |||
| 409 | return null; |
||
| 410 | } |
||
| 411 | |||
| 412 | if (!\is_object($entity)) { |
||
| 413 | throw new \RuntimeException('Invalid argument, object or null required'); |
||
| 414 | } |
||
| 415 | |||
| 416 | if (\in_array($this->getEntityManager($entity)->getUnitOfWork()->getEntityState($entity), [ |
||
| 417 | UnitOfWork::STATE_NEW, |
||
| 418 | UnitOfWork::STATE_REMOVED, |
||
| 419 | ], true)) { |
||
| 420 | // NEXT_MAJOR: Uncomment the following exception, remove the deprecation and the return statement inside this conditional block. |
||
| 421 | // throw new \InvalidArgumentException(sprintf( |
||
| 422 | // 'Can not get the normalized identifier for %s since it is in state %u.', |
||
| 423 | // ClassUtils::getClass($entity), |
||
| 424 | // $this->getEntityManager($entity)->getUnitOfWork()->getEntityState($entity) |
||
| 425 | // )); |
||
| 426 | |||
| 427 | @trigger_error(sprintf( |
||
| 428 | 'Passing an object which is in state %u (new) or %u (removed) as argument 1 for %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.20' |
||
| 429 | .'and will be not allowed in version 4.0.', |
||
| 430 | UnitOfWork::STATE_NEW, |
||
| 431 | UnitOfWork::STATE_REMOVED, |
||
| 432 | __METHOD__ |
||
| 433 | ), E_USER_DEPRECATED); |
||
| 434 | |||
| 435 | return null; |
||
| 436 | } |
||
| 437 | |||
| 438 | $values = $this->getIdentifierValues($entity); |
||
| 439 | |||
| 440 | if (0 === \count($values)) { |
||
| 441 | return null; |
||
| 442 | } |
||
| 443 | |||
| 444 | return implode(self::ID_SEPARATOR, $values); |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * {@inheritdoc} |
||
| 449 | * |
||
| 450 | * The ORM implementation does nothing special but you still should use |
||
| 451 | * this method when using the id in a URL to allow for future improvements. |
||
| 452 | */ |
||
| 453 | public function getUrlSafeIdentifier($entity) |
||
| 454 | { |
||
| 455 | // NEXT_MAJOR: Remove the following check and declare "object" as type for argument 1. |
||
| 456 | if (!\is_object($entity)) { |
||
| 457 | @trigger_error(sprintf( |
||
| 458 | 'Passing other type than object for argument 1 for %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.20 and will be not allowed in version 4.0.', |
||
| 459 | __METHOD__ |
||
| 460 | ), E_USER_DEPRECATED); |
||
| 461 | |||
| 462 | return null; |
||
| 463 | } |
||
| 464 | |||
| 465 | return $this->getNormalizedIdentifier($entity); |
||
| 466 | } |
||
| 467 | |||
| 468 | public function addIdentifiersToQuery($class, ProxyQueryInterface $queryProxy, array $idx) |
||
| 469 | { |
||
| 470 | $fieldNames = $this->getIdentifierFieldNames($class); |
||
| 471 | $qb = $queryProxy->getQueryBuilder(); |
||
| 472 | |||
| 473 | $prefix = uniqid(); |
||
| 474 | $sqls = []; |
||
| 475 | foreach ($idx as $pos => $id) { |
||
| 476 | $ids = explode(self::ID_SEPARATOR, $id); |
||
| 477 | |||
| 478 | $ands = []; |
||
| 479 | foreach ($fieldNames as $posName => $name) { |
||
| 480 | $parameterName = sprintf('field_%s_%s_%d', $prefix, $name, $pos); |
||
| 481 | $ands[] = sprintf('%s.%s = :%s', current($qb->getRootAliases()), $name, $parameterName); |
||
| 482 | $qb->setParameter($parameterName, $ids[$posName]); |
||
| 483 | } |
||
| 484 | |||
| 485 | $sqls[] = implode(' AND ', $ands); |
||
| 486 | } |
||
| 487 | |||
| 488 | $qb->andWhere(sprintf('( %s )', implode(' OR ', $sqls))); |
||
| 489 | } |
||
| 490 | |||
| 491 | public function batchDelete($class, ProxyQueryInterface $queryProxy) |
||
| 492 | { |
||
| 493 | $queryProxy->select('DISTINCT '.current($queryProxy->getRootAliases())); |
||
| 494 | |||
| 495 | try { |
||
| 496 | $entityManager = $this->getEntityManager($class); |
||
| 497 | |||
| 498 | $i = 0; |
||
| 499 | foreach ($queryProxy->getQuery()->iterate() as $pos => $object) { |
||
| 500 | $entityManager->remove($object[0]); |
||
| 501 | |||
| 502 | if (0 === (++$i % 20)) { |
||
| 503 | $entityManager->flush(); |
||
| 504 | $entityManager->clear(); |
||
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | $entityManager->flush(); |
||
| 509 | $entityManager->clear(); |
||
| 510 | } catch (\PDOException | DBALException $e) { |
||
| 511 | throw new ModelManagerException('', 0, $e); |
||
| 512 | } |
||
| 513 | } |
||
| 514 | |||
| 515 | public function getDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null) |
||
| 516 | { |
||
| 517 | $datagrid->buildPager(); |
||
| 518 | $query = $datagrid->getQuery(); |
||
| 519 | |||
| 520 | $query->select('DISTINCT '.current($query->getRootAliases())); |
||
| 521 | $query->setFirstResult($firstResult); |
||
| 522 | $query->setMaxResults($maxResult); |
||
| 523 | |||
| 524 | if ($query instanceof ProxyQueryInterface) { |
||
| 525 | $sortBy = $query->getSortBy(); |
||
| 526 | |||
| 527 | if (!empty($sortBy)) { |
||
| 528 | $query->addOrderBy($sortBy, $query->getSortOrder()); |
||
| 529 | $query = $query->getQuery(); |
||
| 530 | $query->setHint(Query::HINT_CUSTOM_TREE_WALKERS, [OrderByToSelectWalker::class]); |
||
| 531 | } else { |
||
| 532 | $query = $query->getQuery(); |
||
| 533 | } |
||
| 534 | } |
||
| 535 | |||
| 536 | return new DoctrineORMQuerySourceIterator($query, $fields); |
||
| 537 | } |
||
| 538 | |||
| 539 | public function getExportFields($class) |
||
| 540 | { |
||
| 541 | $metadata = $this->getEntityManager($class)->getClassMetadata($class); |
||
| 542 | |||
| 543 | return $metadata->getFieldNames(); |
||
| 544 | } |
||
| 545 | |||
| 546 | public function getModelInstance($class) |
||
| 547 | { |
||
| 548 | $r = new \ReflectionClass($class); |
||
| 549 | if ($r->isAbstract()) { |
||
| 550 | throw new \RuntimeException(sprintf('Cannot initialize abstract class: %s', $class)); |
||
| 551 | } |
||
| 552 | |||
| 553 | $constructor = $r->getConstructor(); |
||
| 554 | |||
| 555 | if (null !== $constructor && (!$constructor->isPublic() || $constructor->getNumberOfRequiredParameters() > 0)) { |
||
| 556 | return $r->newInstanceWithoutConstructor(); |
||
| 557 | } |
||
| 558 | |||
| 559 | return new $class(); |
||
| 560 | } |
||
| 561 | |||
| 562 | public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid) |
||
| 563 | { |
||
| 564 | $values = $datagrid->getValues(); |
||
| 565 | |||
| 566 | if ($this->isFieldAlreadySorted($fieldDescription, $datagrid)) { |
||
| 567 | if ('ASC' === $values['_sort_order']) { |
||
| 568 | $values['_sort_order'] = 'DESC'; |
||
| 569 | } else { |
||
| 570 | $values['_sort_order'] = 'ASC'; |
||
| 571 | } |
||
| 572 | } else { |
||
| 573 | $values['_sort_order'] = 'ASC'; |
||
| 574 | } |
||
| 575 | |||
| 576 | $values['_sort_by'] = \is_string($fieldDescription->getOption('sortable')) ? $fieldDescription->getOption('sortable') : $fieldDescription->getName(); |
||
| 577 | |||
| 578 | return ['filter' => $values]; |
||
| 579 | } |
||
| 580 | |||
| 581 | public function getPaginationParameters(DatagridInterface $datagrid, $page) |
||
| 582 | { |
||
| 583 | $values = $datagrid->getValues(); |
||
| 584 | |||
| 585 | if (isset($values['_sort_by']) && $values['_sort_by'] instanceof FieldDescriptionInterface) { |
||
| 586 | $values['_sort_by'] = $values['_sort_by']->getName(); |
||
| 587 | } |
||
| 588 | $values['_page'] = $page; |
||
| 589 | |||
| 590 | return ['filter' => $values]; |
||
| 591 | } |
||
| 592 | |||
| 593 | public function getDefaultSortValues($class) |
||
| 594 | { |
||
| 595 | return [ |
||
| 596 | '_sort_order' => 'ASC', |
||
| 597 | '_sort_by' => implode(',', $this->getModelIdentifier($class)), |
||
| 598 | '_page' => 1, |
||
| 599 | '_per_page' => 25, |
||
| 600 | ]; |
||
| 601 | } |
||
| 602 | |||
| 603 | public function getDefaultPerPageOptions(string $class): array |
||
| 604 | { |
||
| 605 | return [10, 25, 50, 100, 250]; |
||
| 606 | } |
||
| 607 | |||
| 608 | public function modelTransform($class, $instance) |
||
| 609 | { |
||
| 610 | return $instance; |
||
| 611 | } |
||
| 612 | |||
| 613 | public function modelReverseTransform($class, array $array = []) |
||
| 614 | { |
||
| 615 | $instance = $this->getModelInstance($class); |
||
| 616 | $metadata = $this->getMetadata($class); |
||
| 617 | |||
| 618 | foreach ($array as $name => $value) { |
||
| 619 | // property or association ? |
||
| 620 | if (\array_key_exists($name, $metadata->fieldMappings)) { |
||
| 621 | $property = $metadata->fieldMappings[$name]['fieldName']; |
||
| 622 | } elseif (\array_key_exists($name, $metadata->associationMappings)) { |
||
| 623 | $property = $metadata->associationMappings[$name]['fieldName']; |
||
| 624 | } else { |
||
| 625 | $property = $name; |
||
| 626 | } |
||
| 627 | |||
| 628 | $this->propertyAccessor->setValue($instance, $property, $value); |
||
| 629 | } |
||
| 630 | |||
| 631 | return $instance; |
||
| 632 | } |
||
| 633 | |||
| 634 | public function getModelCollectionInstance($class) |
||
| 635 | { |
||
| 636 | return new \Doctrine\Common\Collections\ArrayCollection(); |
||
| 637 | } |
||
| 638 | |||
| 639 | public function collectionClear(&$collection) |
||
| 640 | { |
||
| 641 | return $collection->clear(); |
||
| 642 | } |
||
| 643 | |||
| 644 | public function collectionHasElement(&$collection, &$element) |
||
| 645 | { |
||
| 646 | return $collection->contains($element); |
||
| 647 | } |
||
| 648 | |||
| 649 | public function collectionAddElement(&$collection, &$element) |
||
| 650 | { |
||
| 651 | return $collection->add($element); |
||
| 652 | } |
||
| 653 | |||
| 654 | public function collectionRemoveElement(&$collection, &$element) |
||
| 655 | { |
||
| 656 | return $collection->removeElement($element); |
||
| 657 | } |
||
| 658 | |||
| 659 | /** |
||
| 660 | * NEXT_MAJOR: Remove this method. |
||
| 661 | * |
||
| 662 | * @param string $property |
||
| 663 | * |
||
| 664 | * @return mixed |
||
| 665 | */ |
||
| 666 | protected function camelize($property) |
||
| 667 | { |
||
| 668 | @trigger_error(sprintf( |
||
| 669 | 'Method %s() is deprecated since sonata-project/doctrine-orm-admin-bundle 3.x and will be removed in version 4.0.', |
||
| 670 | __METHOD__ |
||
| 671 | ), E_USER_DEPRECATED); |
||
| 672 | |||
| 673 | return str_replace(' ', '', ucwords(str_replace('_', ' ', $property))); |
||
| 674 | } |
||
| 675 | |||
| 676 | private function isFieldAlreadySorted(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid): bool |
||
| 687 | |||
| 688 | /** |
||
| 689 | * @param mixed $value |
||
| 690 | */ |
||
| 691 | private function getValueFromType($value, Type $type, string $fieldType, AbstractPlatform $platform): string |
||
| 711 | } |
||
| 712 |
If you suppress an error, we recommend checking for the error condition explicitly: