MongoDocumentWrapper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Gedmo\Tool\Wrapper;
  3. use Doctrine\ODM\MongoDB\DocumentManager;
  4. use Doctrine\ODM\MongoDB\Proxy\Proxy;
  5. /**
  6. * Wraps document 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 MongoDocumentWrapper extends AbstractWrapper
  13. {
  14. /**
  15. * Document identifier
  16. *
  17. * @var mixed
  18. */
  19. private $identifier;
  20. /**
  21. * True if document or proxy is loaded
  22. *
  23. * @var boolean
  24. */
  25. private $initialized = false;
  26. /**
  27. * Wrap document
  28. *
  29. * @param object $document
  30. * @param \Doctrine\ODM\MongoDB\DocumentManager $dm
  31. */
  32. public function __construct($document, DocumentManager $dm)
  33. {
  34. $this->om = $dm;
  35. $this->object = $document;
  36. $this->meta = $dm->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 getRootObjectName()
  50. {
  51. return $this->meta->rootDocumentName;
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function setPropertyValue($property, $value)
  57. {
  58. $this->initialize();
  59. $this->meta->getReflectionProperty($property)->setValue($this->object, $value);
  60. return $this;
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function hasValidIdentifier()
  66. {
  67. return (bool)$this->getIdentifier();
  68. }
  69. /**
  70. * {@inheritDoc}
  71. */
  72. public function getIdentifier($single = true)
  73. {
  74. if (!$this->identifier) {
  75. if ($this->object instanceof Proxy) {
  76. $uow = $this->om->getUnitOfWork();
  77. if ($uow->isInIdentityMap($this->object)) {
  78. $this->identifier = (string)$uow->getDocumentIdentifier($this->object);
  79. } else {
  80. $this->initialize();
  81. }
  82. }
  83. if (!$this->identifier) {
  84. $this->identifier = (string)$this->getPropertyValue($this->meta->identifier);
  85. }
  86. }
  87. return $this->identifier;
  88. }
  89. /**
  90. * Initialize the document if it is proxy
  91. * required when is detached or not initialized
  92. */
  93. protected function initialize()
  94. {
  95. if (!$this->initialized) {
  96. if ($this->object instanceof Proxy) {
  97. $uow = $this->om->getUnitOfWork();
  98. if (!$this->object->__isInitialized__) {
  99. $persister = $uow->getDocumentPersister($this->meta->name);
  100. $identifier = null;
  101. if ($uow->isInIdentityMap($this->object)) {
  102. $identifier = $this->getIdentifier();
  103. } else {
  104. // this may not happen but in case
  105. $reflProperty = new \ReflectionProperty($this->object, 'identifier');
  106. $reflProperty->setAccessible(true);
  107. $identifier = $reflProperty->getValue($this->object);
  108. }
  109. $this->object->__isInitialized__ = true;
  110. $persister->load($identifier, $this->object);
  111. }
  112. }
  113. }
  114. }
  115. }