EntityWrapper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 (bool)$this->getIdentifier();
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. public function getIdentifier($single = true)
  69. {
  70. if (!$this->identifier) {
  71. if ($this->object instanceof Proxy) {
  72. $uow = $this->om->getUnitOfWork();
  73. if ($uow->isInIdentityMap($this->object)) {
  74. $this->identifier = $uow->getEntityIdentifier($this->object);
  75. } else {
  76. $this->initialize();
  77. }
  78. }
  79. if (!$this->identifier) {
  80. $this->identifier = array();
  81. $incomplete = false;
  82. foreach ($this->meta->identifier as $name) {
  83. $this->identifier[$name] = $this->getPropertyValue($name);
  84. if (!$this->identifier[$name]) {
  85. $incomplete = true;
  86. }
  87. }
  88. if ($incomplete) {
  89. $this->identifier = null;
  90. }
  91. }
  92. }
  93. if ($single && is_array($this->identifier)) {
  94. return reset($this->identifier);
  95. }
  96. return $this->identifier;
  97. }
  98. /**
  99. * Initialize the entity if it is proxy
  100. * required when is detached or not initialized
  101. */
  102. protected function initialize()
  103. {
  104. if (!$this->initialized) {
  105. if ($this->object instanceof Proxy) {
  106. $uow = $this->om->getUnitOfWork();
  107. if (!$this->object->__isInitialized__) {
  108. $this->object->__load();
  109. }
  110. }
  111. }
  112. }
  113. }