EntityWrapper.php 3.2KB

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