AssignedGenerator.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Id;
  20. use Doctrine\ORM\EntityManager;
  21. use Doctrine\ORM\ORMException;
  22. /**
  23. * Special generator for application-assigned identifiers (doesnt really generate anything).
  24. *
  25. * @since 2.0
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  28. * @author Jonathan Wage <jonwage@gmail.com>
  29. * @author Roman Borschel <roman@code-factory.org>
  30. */
  31. class AssignedGenerator extends AbstractIdGenerator
  32. {
  33. /**
  34. * Returns the identifier assigned to the given entity.
  35. *
  36. * @param object $entity
  37. * @return mixed
  38. * @override
  39. */
  40. public function generate(EntityManager $em, $entity)
  41. {
  42. $class = $em->getClassMetadata(get_class($entity));
  43. $identifier = array();
  44. if ($class->isIdentifierComposite) {
  45. $idFields = $class->getIdentifierFieldNames();
  46. foreach ($idFields as $idField) {
  47. $value = $class->reflFields[$idField]->getValue($entity);
  48. if (isset($value)) {
  49. if (isset($class->associationMappings[$idField])) {
  50. if (!$em->getUnitOfWork()->isInIdentityMap($value)) {
  51. throw ORMException::entityMissingForeignAssignedId($entity, $value);
  52. }
  53. // NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced.
  54. $identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value));
  55. } else {
  56. $identifier[$idField] = $value;
  57. }
  58. } else {
  59. throw ORMException::entityMissingAssignedId($entity);
  60. }
  61. }
  62. } else {
  63. $idField = $class->identifier[0];
  64. $value = $class->reflFields[$idField]->getValue($entity);
  65. if (isset($value)) {
  66. if (isset($class->associationMappings[$idField])) {
  67. if (!$em->getUnitOfWork()->isInIdentityMap($value)) {
  68. throw ORMException::entityMissingForeignAssignedId($entity, $value);
  69. }
  70. // NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced.
  71. $identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value));
  72. } else {
  73. $identifier[$idField] = $value;
  74. }
  75. } else {
  76. throw ORMException::entityMissingAssignedId($entity);
  77. }
  78. }
  79. return $identifier;
  80. }
  81. }