ORM.php 4.5 KB

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