EntityWrapper.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Gedmo\Tool\Wrapper;
  3. use Doctrine\ORM\EntityManager;
  4. use Doctrine\ORM\Proxy\Proxy;
  5. /**
  6. * Wraps entity or proxy for more convenient
  7. * manipulation
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  11. */
  12. class EntityWrapper extends AbstractWrapper
  13. {
  14. /**
  15. * Entity identifier
  16. *
  17. * @var array
  18. */
  19. private $identifier;
  20. /**
  21. * True if entity or proxy is loaded
  22. *
  23. * @var boolean
  24. */
  25. private $initialized = false;
  26. /**
  27. * Wrap entity
  28. *
  29. * @param object $entity
  30. * @param \Doctrine\ORM\EntityManager $em
  31. */
  32. public function __construct($entity, EntityManager $em)
  33. {
  34. $this->om = $em;
  35. $this->object = $entity;
  36. $this->meta = $em->getClassMetadata(get_class($this->object));
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function getPropertyValue($property)
  42. {
  43. $this->initialize();
  44. return $this->meta->getReflectionProperty($property)->getValue($this->object);
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function setPropertyValue($property, $value)
  50. {
  51. $this->initialize();
  52. $this->meta->getReflectionProperty($property)->setValue($this->object, $value);
  53. return $this;
  54. }
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function hasValidIdentifier()
  59. {
  60. return (null !== $this->getIdentifier());
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function getRootObjectName()
  66. {
  67. return $this->meta->rootEntityName;
  68. }
  69. /**
  70. * {@inheritDoc}
  71. */
  72. public function getIdentifier($single = true)
  73. {
  74. if (null === $this->identifier) {
  75. if ($this->object instanceof Proxy) {
  76. $uow = $this->om->getUnitOfWork();
  77. if ($uow->isInIdentityMap($this->object)) {
  78. $this->identifier = $uow->getEntityIdentifier($this->object);
  79. } else {
  80. $this->initialize();
  81. }
  82. }
  83. if (null === $this->identifier) {
  84. $this->identifier = array();
  85. $incomplete = false;
  86. foreach ($this->meta->identifier as $name) {
  87. $this->identifier[$name] = $this->getPropertyValue($name);
  88. if (null === $this->identifier[$name]) {
  89. $incomplete = true;
  90. }
  91. }
  92. if ($incomplete) {
  93. $this->identifier = null;
  94. }
  95. }
  96. }
  97. if ($single && is_array($this->identifier)) {
  98. return reset($this->identifier);
  99. }
  100. return $this->identifier;
  101. }
  102. /**
  103. * Initialize the entity if it is proxy
  104. * required when is detached or not initialized
  105. */
  106. protected function initialize()
  107. {
  108. if (!$this->initialized) {
  109. if ($this->object instanceof Proxy) {
  110. $uow = $this->om->getUnitOfWork();
  111. if (!$this->object->__isInitialized__) {
  112. $this->object->__load();
  113. }
  114. }
  115. }
  116. }
  117. }