IdReader.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * @return bool returns `true` if the class has a single-column ID and
  54. * `false` otherwise
  55. */
  56. public function isSingleId(): bool
  57. {
  58. return $this->singleId;
  59. }
  60. /**
  61. * Returns whether the class has a single-column integer ID.
  62. *
  63. * @return bool returns `true` if the class has a single-column integer ID
  64. * and `false` otherwise
  65. */
  66. public function isIntId(): bool
  67. {
  68. return $this->intId;
  69. }
  70. /**
  71. * Returns the ID value for an object.
  72. *
  73. * This method assumes that the object has a single-column ID.
  74. *
  75. * @return string The ID value
  76. */
  77. public function getIdValue(object $object = null)
  78. {
  79. if (!$object) {
  80. return '';
  81. }
  82. if (!$this->om->contains($object)) {
  83. 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)));
  84. }
  85. $this->om->initializeObject($object);
  86. $idValue = current($this->classMetadata->getIdentifierValues($object));
  87. if ($this->associationIdReader) {
  88. $idValue = $this->associationIdReader->getIdValue($idValue);
  89. }
  90. return (string) $idValue;
  91. }
  92. /**
  93. * Returns the name of the ID field.
  94. *
  95. * This method assumes that the object has a single-column ID.
  96. *
  97. * @return string The name of the ID field
  98. */
  99. public function getIdField(): string
  100. {
  101. return $this->idField;
  102. }
  103. }