IdentifierFlattener.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\Mapping\ClassMetadata;
  21. use Doctrine\ORM\UnitOfWork;
  22. use Doctrine\Persistence\Mapping\ClassMetadataFactory;
  23. use function assert;
  24. use function implode;
  25. use function is_object;
  26. /**
  27. * The IdentifierFlattener utility now houses some of the identifier manipulation logic from unit of work, so that it
  28. * can be re-used elsewhere.
  29. */
  30. final class IdentifierFlattener
  31. {
  32. /**
  33. * The UnitOfWork used to coordinate object-level transactions.
  34. *
  35. * @var UnitOfWork
  36. */
  37. private $unitOfWork;
  38. /**
  39. * The metadata factory, used to retrieve the ORM metadata of entity classes.
  40. *
  41. * @var ClassMetadataFactory
  42. */
  43. private $metadataFactory;
  44. /**
  45. * Initializes a new IdentifierFlattener instance, bound to the given EntityManager.
  46. */
  47. public function __construct(UnitOfWork $unitOfWork, ClassMetadataFactory $metadataFactory)
  48. {
  49. $this->unitOfWork = $unitOfWork;
  50. $this->metadataFactory = $metadataFactory;
  51. }
  52. /**
  53. * convert foreign identifiers into scalar foreign key values to avoid object to string conversion failures.
  54. *
  55. * @param mixed[] $id
  56. *
  57. * @psalm-return array<string, mixed>
  58. */
  59. public function flattenIdentifier(ClassMetadata $class, array $id)
  60. {
  61. $flatId = [];
  62. foreach ($class->identifier as $field) {
  63. if (isset($class->associationMappings[$field]) && isset($id[$field]) && is_object($id[$field])) {
  64. $targetClassMetadata = $this->metadataFactory->getMetadataFor(
  65. $class->associationMappings[$field]['targetEntity']
  66. );
  67. assert($targetClassMetadata instanceof ClassMetadata);
  68. if ($this->unitOfWork->isInIdentityMap($id[$field])) {
  69. $associatedId = $this->flattenIdentifier($targetClassMetadata, $this->unitOfWork->getEntityIdentifier($id[$field]));
  70. } else {
  71. $associatedId = $this->flattenIdentifier($targetClassMetadata, $targetClassMetadata->getIdentifierValues($id[$field]));
  72. }
  73. $flatId[$field] = implode(' ', $associatedId);
  74. } elseif (isset($class->associationMappings[$field])) {
  75. $associatedId = [];
  76. foreach ($class->associationMappings[$field]['joinColumns'] as $joinColumn) {
  77. $associatedId[] = $id[$joinColumn['name']];
  78. }
  79. $flatId[$field] = implode(' ', $associatedId);
  80. } else {
  81. $flatId[$field] = $id[$field];
  82. }
  83. }
  84. return $flatId;
  85. }
  86. }