ObjectManager.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Doctrine\Persistence;
  3. use Doctrine\Persistence\Mapping\ClassMetadata;
  4. use Doctrine\Persistence\Mapping\ClassMetadataFactory;
  5. /**
  6. * Contract for a Doctrine persistence layer ObjectManager class to implement.
  7. */
  8. interface ObjectManager
  9. {
  10. /**
  11. * Finds an object by its identifier.
  12. *
  13. * This is just a convenient shortcut for getRepository($className)->find($id).
  14. *
  15. * @param string $className The class name of the object to find.
  16. * @param mixed $id The identity of the object to find.
  17. *
  18. * @return object|null The found object.
  19. *
  20. * @template T
  21. * @psalm-param class-string<T> $className
  22. * @psalm-return T|null
  23. */
  24. public function find($className, $id);
  25. /**
  26. * Tells the ObjectManager to make an instance managed and persistent.
  27. *
  28. * The object will be entered into the database as a result of the flush operation.
  29. *
  30. * NOTE: The persist operation always considers objects that are not yet known to
  31. * this ObjectManager as NEW. Do not pass detached objects to the persist operation.
  32. *
  33. * @param object $object The instance to make managed and persistent.
  34. *
  35. * @return void
  36. */
  37. public function persist($object);
  38. /**
  39. * Removes an object instance.
  40. *
  41. * A removed object will be removed from the database as a result of the flush operation.
  42. *
  43. * @param object $object The object instance to remove.
  44. *
  45. * @return void
  46. */
  47. public function remove($object);
  48. /**
  49. * Merges the state of a detached object into the persistence context
  50. * of this ObjectManager and returns the managed copy of the object.
  51. * The object passed to merge will not become associated/managed with this ObjectManager.
  52. *
  53. * @deprecated Merge operation is deprecated and will be removed in Persistence 2.0.
  54. * Merging should be part of the business domain of an application rather than
  55. * a generic operation of ObjectManager.
  56. *
  57. * @param object $object
  58. *
  59. * @return object
  60. */
  61. public function merge($object);
  62. /**
  63. * Clears the ObjectManager. All objects that are currently managed
  64. * by this ObjectManager become detached.
  65. *
  66. * @param string|null $objectName if given, only objects of this type will get detached.
  67. *
  68. * @return void
  69. */
  70. public function clear($objectName = null);
  71. /**
  72. * Detaches an object from the ObjectManager, causing a managed object to
  73. * become detached. Unflushed changes made to the object if any
  74. * (including removal of the object), will not be synchronized to the database.
  75. * Objects which previously referenced the detached object will continue to
  76. * reference it.
  77. *
  78. * @deprecated Detach operation is deprecated and will be removed in Persistence 2.0. Please use
  79. * {@see ObjectManager::clear()} instead.
  80. *
  81. * @param object $object The object to detach.
  82. *
  83. * @return void
  84. */
  85. public function detach($object);
  86. /**
  87. * Refreshes the persistent state of an object from the database,
  88. * overriding any local changes that have not yet been persisted.
  89. *
  90. * @param object $object The object to refresh.
  91. *
  92. * @return void
  93. */
  94. public function refresh($object);
  95. /**
  96. * Flushes all changes to objects that have been queued up to now to the database.
  97. * This effectively synchronizes the in-memory state of managed objects with the
  98. * database.
  99. *
  100. * @return void
  101. */
  102. public function flush();
  103. /**
  104. * Gets the repository for a class.
  105. *
  106. * @param string $className
  107. *
  108. * @return ObjectRepository
  109. *
  110. * @template T
  111. * @psalm-param class-string<T> $className
  112. * @psalm-return ObjectRepository<T>
  113. */
  114. public function getRepository($className);
  115. /**
  116. * Returns the ClassMetadata descriptor for a class.
  117. *
  118. * The class name must be the fully-qualified class name without a leading backslash
  119. * (as it is returned by get_class($obj)).
  120. *
  121. * @param string $className
  122. *
  123. * @return ClassMetadata
  124. */
  125. public function getClassMetadata($className);
  126. /**
  127. * Gets the metadata factory used to gather the metadata of classes.
  128. *
  129. * @return ClassMetadataFactory
  130. */
  131. public function getMetadataFactory();
  132. /**
  133. * Helper method to initialize a lazy loading proxy or persistent collection.
  134. *
  135. * This method is a no-op for other objects.
  136. *
  137. * @param object $obj
  138. *
  139. * @return void
  140. */
  141. public function initializeObject($obj);
  142. /**
  143. * Checks if the object is part of the current UnitOfWork and therefore managed.
  144. *
  145. * @param object $object
  146. *
  147. * @return bool
  148. */
  149. public function contains($object);
  150. }