ODM.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Gedmo\Mapping\Event\Adapter;
  3. use Gedmo\Mapping\Event\AdapterInterface;
  4. use Doctrine\Common\EventArgs;
  5. use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
  6. use Doctrine\ODM\MongoDB\UnitOfWork;
  7. use Doctrine\ODM\MongoDB\DocumentManager;
  8. use Doctrine\ODM\MongoDB\Proxy\Proxy;
  9. /**
  10. * Doctrine event adapter for ODM specific
  11. * event arguments
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Mapping.Event.Adapter
  15. * @subpackage ODM
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class ODM implements AdapterInterface
  20. {
  21. /**
  22. * @var EventArgs
  23. */
  24. private $args;
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function setEventArgs(EventArgs $args)
  29. {
  30. $this->args = $args;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getDomainObjectName()
  36. {
  37. return 'Document';
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getManagerName()
  43. {
  44. return 'ODM';
  45. }
  46. /**
  47. * Extracts identifiers from object or proxy
  48. *
  49. * @param DocumentManager $dm
  50. * @param object $object
  51. * @param bool $single
  52. * @return mixed - array or single identifier
  53. */
  54. public function extractIdentifier(DocumentManager $dm, $object, $single = true)
  55. {
  56. if ($object instanceof Proxy) {
  57. $id = $dm->getUnitOfWork()->getDocumentIdentifier($object);
  58. } else {
  59. $meta = $dm->getClassMetadata(get_class($object));
  60. $id = $meta->getReflectionProperty($meta->identifier)->getValue($object);
  61. }
  62. if ($single) {
  63. return $id;
  64. } else {
  65. return array($meta->identifier => $id);
  66. }
  67. }
  68. /**
  69. * Call event specific method
  70. *
  71. * @param string $method
  72. * @param array $args
  73. * @return mixed
  74. */
  75. public function __call($method, $args)
  76. {
  77. $method = str_replace('Object', $this->getDomainObjectName(), $method);
  78. return call_user_func_array(array($this->args, $method), $args);
  79. }
  80. /**
  81. * Get the object changeset from a UnitOfWork
  82. *
  83. * @param UnitOfWork $uow
  84. * @param Object $object
  85. * @return array
  86. */
  87. public function getObjectChangeSet(UnitOfWork $uow, $object)
  88. {
  89. return $uow->getDocumentChangeSet($object);
  90. }
  91. /**
  92. * Get the single identifier field name
  93. *
  94. * @param ClassMetadataInfo $meta
  95. * @throws MappingException - if identifier is composite
  96. * @return string
  97. */
  98. public function getSingleIdentifierFieldName(ClassMetadataInfo $meta)
  99. {
  100. return $meta->identifier;
  101. }
  102. /**
  103. * Recompute the single object changeset from a UnitOfWork
  104. *
  105. * @param UnitOfWork $uow
  106. * @param ClassMetadataInfo $meta
  107. * @param Object $object
  108. * @return void
  109. */
  110. public function recomputeSingleObjectChangeSet(UnitOfWork $uow, ClassMetadataInfo $meta, $object)
  111. {
  112. $uow->recomputeSingleDocumentChangeSet($meta, $object);
  113. }
  114. /**
  115. * Get the scheduled object updates from a UnitOfWork
  116. *
  117. * @param UnitOfWork $uow
  118. * @return array
  119. */
  120. public function getScheduledObjectUpdates(UnitOfWork $uow)
  121. {
  122. return $uow->getScheduledDocumentUpdates();
  123. }
  124. /**
  125. * Get the scheduled object insertions from a UnitOfWork
  126. *
  127. * @param UnitOfWork $uow
  128. * @return array
  129. */
  130. public function getScheduledObjectInsertions(UnitOfWork $uow)
  131. {
  132. return $uow->getScheduledDocumentInsertions();
  133. }
  134. /**
  135. * Get the scheduled object deletions from a UnitOfWork
  136. *
  137. * @param UnitOfWork $uow
  138. * @return array
  139. */
  140. public function getScheduledObjectDeletions(UnitOfWork $uow)
  141. {
  142. return $uow->getScheduledDocumentDeletions();
  143. }
  144. /**
  145. * Sets a property value of the original data array of an object
  146. *
  147. * @param UnitOfWork $uow
  148. * @param string $oid
  149. * @param string $property
  150. * @param mixed $value
  151. * @return void
  152. */
  153. public function setOriginalObjectProperty(UnitOfWork $uow, $oid, $property, $value)
  154. {
  155. $uow->setOriginalDocumentProperty($oid, $property, $value);
  156. }
  157. /**
  158. * Clears the property changeset of the object with the given OID.
  159. *
  160. * @param UnitOfWork $uow
  161. * @param string $oid The object's OID.
  162. */
  163. public function clearObjectChangeSet(UnitOfWork $uow, $oid)
  164. {
  165. $uow->clearDocumentChangeSet($oid);
  166. }
  167. }