ORM.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. // return null if one of identifiers is missing
  64. if (!$id[$name]) {
  65. return null;
  66. }
  67. }
  68. }
  69. if ($single) {
  70. $id = current($id);
  71. }
  72. return $id;
  73. }
  74. /**
  75. * Call event specific method
  76. *
  77. * @param string $method
  78. * @param array $args
  79. * @return mixed
  80. */
  81. public function __call($method, $args)
  82. {
  83. $method = str_replace('Object', $this->getDomainObjectName(), $method);
  84. return call_user_func_array(array($this->args, $method), $args);
  85. }
  86. /**
  87. * Get the object changeset from a UnitOfWork
  88. *
  89. * @param UnitOfWork $uow
  90. * @param Object $object
  91. * @return array
  92. */
  93. public function getObjectChangeSet(UnitOfWork $uow, $object)
  94. {
  95. return $uow->getEntityChangeSet($object);
  96. }
  97. /**
  98. * Get the single identifier field name
  99. *
  100. * @param ClassMetadataInfo $meta
  101. * @throws MappingException - if identifier is composite
  102. * @return string
  103. */
  104. public function getSingleIdentifierFieldName(ClassMetadataInfo $meta)
  105. {
  106. return $meta->getSingleIdentifierFieldName();
  107. }
  108. /**
  109. * Recompute the single object changeset from a UnitOfWork
  110. *
  111. * @param UnitOfWork $uow
  112. * @param ClassMetadataInfo $meta
  113. * @param Object $object
  114. * @return void
  115. */
  116. public function recomputeSingleObjectChangeSet(UnitOfWork $uow, ClassMetadataInfo $meta, $object)
  117. {
  118. $uow->recomputeSingleEntityChangeSet($meta, $object);
  119. }
  120. /**
  121. * Get the scheduled object updates from a UnitOfWork
  122. *
  123. * @param UnitOfWork $uow
  124. * @return array
  125. */
  126. public function getScheduledObjectUpdates(UnitOfWork $uow)
  127. {
  128. return $uow->getScheduledEntityUpdates();
  129. }
  130. /**
  131. * Get the scheduled object insertions from a UnitOfWork
  132. *
  133. * @param UnitOfWork $uow
  134. * @return array
  135. */
  136. public function getScheduledObjectInsertions(UnitOfWork $uow)
  137. {
  138. return $uow->getScheduledEntityInsertions();
  139. }
  140. /**
  141. * Get the scheduled object deletions from a UnitOfWork
  142. *
  143. * @param UnitOfWork $uow
  144. * @return array
  145. */
  146. public function getScheduledObjectDeletions(UnitOfWork $uow)
  147. {
  148. return $uow->getScheduledEntityDeletions();
  149. }
  150. /**
  151. * Sets a property value of the original data array of an object
  152. *
  153. * @param UnitOfWork $uow
  154. * @param string $oid
  155. * @param string $property
  156. * @param mixed $value
  157. * @return void
  158. */
  159. public function setOriginalObjectProperty(UnitOfWork $uow, $oid, $property, $value)
  160. {
  161. $uow->setOriginalEntityProperty($oid, $property, $value);
  162. }
  163. /**
  164. * Clears the property changeset of the object with the given OID.
  165. *
  166. * @param UnitOfWork $uow
  167. * @param string $oid The object's OID.
  168. */
  169. public function clearObjectChangeSet(UnitOfWork $uow, $oid)
  170. {
  171. $uow->clearEntityChangeSet($oid);
  172. }
  173. }