Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppBundle\Manager;
  4.  
  5. use Doctrine\ORM\EntityManager;
  6.  
  7. /**
  8. * Class BaseManager.
  9. */
  10. class BaseManager
  11. {
  12. /**
  13. * @var \Doctrine\ORM\EntityManager
  14. */
  15. protected $em;
  16.  
  17. /**
  18. * @var EntityRepository
  19. */
  20. protected $repository;
  21.  
  22. /**
  23. * @var string Class name
  24. */
  25. protected $class;
  26.  
  27. /**
  28. * Constructor.
  29. *
  30. * @param EntityManager $em Entity manager
  31. * @param string $class Class name
  32. */
  33. public function __construct(EntityManager $em, $class)
  34. {
  35. $this->class = $class;
  36. $this->em = $em;
  37. $this->repository = $em->getRepository($this->class);
  38. }
  39.  
  40. /**
  41. * Returns a new non-managed entity.
  42. *
  43. * @return mixed
  44. */
  45. public function create()
  46. {
  47. return new $this->class();
  48. }
  49.  
  50. /**
  51. * Returns a "fresh" entity by identifier.
  52. *
  53. * @param int $id Entity identifier
  54. *
  55. * @return object
  56. */
  57. public function find($id)
  58. {
  59. return $this->repository->findOneById($id);
  60. }
  61.  
  62. /**
  63. * Returns an entity by id.
  64. *
  65. * @param int $id Entity id
  66. *
  67. * @return object
  68. */
  69. public function findOneById($id)
  70. {
  71. return $this->repository->findOneById($id);
  72. }
  73.  
  74. /**
  75. * Returns all entities of the child class.
  76. *
  77. * @return array
  78. */
  79. public function findAll()
  80. {
  81. return $this->repository->findAll();
  82. }
  83.  
  84. /**
  85. * Returns entities found for given criteria.
  86. *
  87. * @param array $criteria
  88. *
  89. * @return object
  90. */
  91. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  92. {
  93. return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
  94. }
  95.  
  96. /**
  97. * Returns entity found for given criteria.
  98. *
  99. * @param array $criteria
  100. *
  101. * @return object
  102. */
  103. public function findOneBy(array $criteria)
  104. {
  105. return $this->repository->findOneBy($criteria);
  106. }
  107.  
  108. /**
  109. * Flush persisted entities.
  110. */
  111. public function flush()
  112. {
  113. $this->em->flush();
  114. }
  115.  
  116. /**
  117. * Refresh persisted entities.
  118. */
  119. public function refresh($entity)
  120. {
  121. $this->em->refresh($entity);
  122. }
  123.  
  124. /**
  125. * Clears the repository, causing all managed entities to become detached.
  126. */
  127. public function clear()
  128. {
  129. $this->em->clear();
  130. }
  131.  
  132. /**
  133. * Persist the given entity.
  134. *
  135. * @param mixed $entity An entity instance
  136. * @param bool $doFlush Also flush entity manager?
  137. */
  138. public function save($entity, $doFlush = true)
  139. {
  140. $this->em->persist($entity);
  141.  
  142. if ($doFlush) {
  143. $this->em->flush();
  144. }
  145. }
  146.  
  147. /**
  148. * Delete the given entity.
  149. *
  150. * @param object $entity An entity instance
  151. */
  152. public function remove($entity)
  153. {
  154. $this->em->remove($entity);
  155. $this->em->flush();
  156. }
  157.  
  158. /**
  159. * Returns entity repository.
  160. *
  161. * @return \Doctrine\ORM\EntityRepository|EntityRepository
  162. */
  163. public function getRepository()
  164. {
  165. return $this->repository;
  166. }
  167.  
  168. /**
  169. * @return EntityManager
  170. */
  171. public function getEntityManager()
  172. {
  173. return $this->em;
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement