PersisterHelper.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Utility;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use Doctrine\ORM\Mapping\ClassMetadata;
  22. use Doctrine\ORM\Query\QueryException;
  23. use RuntimeException;
  24. use function sprintf;
  25. /**
  26. * The PersisterHelper contains logic to infer binding types which is used in
  27. * several persisters.
  28. *
  29. * @link www.doctrine-project.org
  30. */
  31. class PersisterHelper
  32. {
  33. /**
  34. * @param string $fieldName
  35. *
  36. * @return array<int, string>
  37. *
  38. * @throws QueryException
  39. */
  40. public static function getTypeOfField($fieldName, ClassMetadata $class, EntityManagerInterface $em)
  41. {
  42. if (isset($class->fieldMappings[$fieldName])) {
  43. return [$class->fieldMappings[$fieldName]['type']];
  44. }
  45. if (! isset($class->associationMappings[$fieldName])) {
  46. return [];
  47. }
  48. $assoc = $class->associationMappings[$fieldName];
  49. if (! $assoc['isOwningSide']) {
  50. return self::getTypeOfField($assoc['mappedBy'], $em->getClassMetadata($assoc['targetEntity']), $em);
  51. }
  52. if ($assoc['type'] & ClassMetadata::MANY_TO_MANY) {
  53. $joinData = $assoc['joinTable'];
  54. } else {
  55. $joinData = $assoc;
  56. }
  57. $types = [];
  58. $targetClass = $em->getClassMetadata($assoc['targetEntity']);
  59. foreach ($joinData['joinColumns'] as $joinColumn) {
  60. $types[] = self::getTypeOfColumn($joinColumn['referencedColumnName'], $targetClass, $em);
  61. }
  62. return $types;
  63. }
  64. /**
  65. * @param string $columnName
  66. *
  67. * @return string
  68. *
  69. * @throws RuntimeException
  70. */
  71. public static function getTypeOfColumn($columnName, ClassMetadata $class, EntityManagerInterface $em)
  72. {
  73. if (isset($class->fieldNames[$columnName])) {
  74. $fieldName = $class->fieldNames[$columnName];
  75. if (isset($class->fieldMappings[$fieldName])) {
  76. return $class->fieldMappings[$fieldName]['type'];
  77. }
  78. }
  79. // iterate over to-one association mappings
  80. foreach ($class->associationMappings as $assoc) {
  81. if (! isset($assoc['joinColumns'])) {
  82. continue;
  83. }
  84. foreach ($assoc['joinColumns'] as $joinColumn) {
  85. if ($joinColumn['name'] === $columnName) {
  86. $targetColumnName = $joinColumn['referencedColumnName'];
  87. $targetClass = $em->getClassMetadata($assoc['targetEntity']);
  88. return self::getTypeOfColumn($targetColumnName, $targetClass, $em);
  89. }
  90. }
  91. }
  92. // iterate over to-many association mappings
  93. foreach ($class->associationMappings as $assoc) {
  94. if (! (isset($assoc['joinTable']) && isset($assoc['joinTable']['joinColumns']))) {
  95. continue;
  96. }
  97. foreach ($assoc['joinTable']['joinColumns'] as $joinColumn) {
  98. if ($joinColumn['name'] === $columnName) {
  99. $targetColumnName = $joinColumn['referencedColumnName'];
  100. $targetClass = $em->getClassMetadata($assoc['targetEntity']);
  101. return self::getTypeOfColumn($targetColumnName, $targetClass, $em);
  102. }
  103. }
  104. }
  105. throw new RuntimeException(sprintf(
  106. 'Could not resolve type of column "%s" of class "%s"',
  107. $columnName,
  108. $class->getName()
  109. ));
  110. }
  111. }