vendor/symfony/doctrine-bridge/Form/ChoiceList/IdReader.php line 25

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\Form\ChoiceList;
  11. use Doctrine\Persistence\Mapping\ClassMetadata;
  12. use Doctrine\Persistence\ObjectManager;
  13. use Symfony\Component\Form\Exception\RuntimeException;
  14. /**
  15. * A utility for reading object IDs.
  16. *
  17. * @author Bernhard Schussek <bschussek@gmail.com>
  18. *
  19. * @internal
  20. */
  21. class IdReader
  22. {
  23. private $om;
  24. private $classMetadata;
  25. private $singleId;
  26. private $intId;
  27. private $idField;
  28. /**
  29. * @var IdReader|null
  30. */
  31. private $associationIdReader;
  32. public function __construct(ObjectManager $om, ClassMetadata $classMetadata)
  33. {
  34. $ids = $classMetadata->getIdentifierFieldNames();
  35. $idType = $classMetadata->getTypeOfField(current($ids));
  36. $this->om = $om;
  37. $this->classMetadata = $classMetadata;
  38. $this->singleId = 1 === \count($ids);
  39. $this->intId = $this->singleId && \in_array($idType, ['integer', 'smallint', 'bigint']);
  40. $this->idField = current($ids);
  41. // single field association are resolved, since the schema column could be an int
  42. if ($this->singleId && $classMetadata->hasAssociation($this->idField)) {
  43. $this->associationIdReader = new self($om, $om->getClassMetadata(
  44. $classMetadata->getAssociationTargetClass($this->idField)
  45. ));
  46. $this->singleId = $this->associationIdReader->isSingleId();
  47. $this->intId = $this->associationIdReader->isIntId();
  48. }
  49. }
  50. /**
  51. * Returns whether the class has a single-column ID.
  52. */
  53. public function isSingleId(): bool
  54. {
  55. return $this->singleId;
  56. }
  57. /**
  58. * Returns whether the class has a single-column integer ID.
  59. */
  60. public function isIntId(): bool
  61. {
  62. return $this->intId;
  63. }
  64. /**
  65. * Returns the ID value for an object.
  66. *
  67. * This method assumes that the object has a single-column ID.
  68. */
  69. public function getIdValue(?object $object = null): string
  70. {
  71. if (!$object) {
  72. return '';
  73. }
  74. if (!$this->om->contains($object)) {
  75. throw new RuntimeException(sprintf('Entity of type "%s" passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?', get_debug_type($object)));
  76. }
  77. $this->om->initializeObject($object);
  78. $idValue = current($this->classMetadata->getIdentifierValues($object));
  79. if ($this->associationIdReader) {
  80. $idValue = $this->associationIdReader->getIdValue($idValue);
  81. }
  82. return (string) $idValue;
  83. }
  84. /**
  85. * Returns the name of the ID field.
  86. *
  87. * This method assumes that the object has a single-column ID.
  88. */
  89. public function getIdField(): string
  90. {
  91. return $this->idField;
  92. }
  93. }