BaseManager.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\CoreBundle\Model;
  11. use Doctrine\Common\Persistence\ManagerRegistry;
  12. use Doctrine\Common\Persistence\ObjectManager;
  13. use Doctrine\Common\Persistence\ObjectRepository;
  14. /**
  15. * @author Hugo Briand <briand@ekino.com>
  16. */
  17. abstract class BaseManager implements ManagerInterface
  18. {
  19. /**
  20. * @var ManagerRegistry
  21. */
  22. protected $registry;
  23. /**
  24. * @var string
  25. */
  26. protected $class;
  27. /**
  28. * @param string $class
  29. * @param ManagerRegistry $registry
  30. */
  31. public function __construct($class, ManagerRegistry $registry)
  32. {
  33. $this->registry = $registry;
  34. $this->class = $class;
  35. }
  36. /**
  37. * @return ObjectManager
  38. */
  39. public function getObjectManager()
  40. {
  41. $manager = $this->registry->getManagerForClass($this->class);
  42. if (!$manager) {
  43. throw new \RuntimeException(sprintf(
  44. 'Unable to find the mapping information for the class %s.'
  45. .' Please check the `auto_mapping` option'
  46. .' (http://symfony.com/doc/current/reference/configuration/doctrine.html#configuration-overview)'
  47. .' or add the bundle to the `mappings` section in the doctrine configuration.',
  48. $this->class
  49. ));
  50. }
  51. return $manager;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getClass()
  57. {
  58. return $this->class;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function findAll()
  64. {
  65. return $this->getRepository()->findAll();
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  71. {
  72. return $this->getRepository()->findBy($criteria, $orderBy, $limit, $offset);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function findOneBy(array $criteria, array $orderBy = null)
  78. {
  79. return $this->getRepository()->findOneBy($criteria, $orderBy);
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function find($id)
  85. {
  86. return $this->getRepository()->find($id);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function create()
  92. {
  93. return new $this->class();
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function save($entity, $andFlush = true)
  99. {
  100. $this->checkObject($entity);
  101. $this->getObjectManager()->persist($entity);
  102. if ($andFlush) {
  103. $this->getObjectManager()->flush();
  104. }
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function delete($entity, $andFlush = true)
  110. {
  111. $this->checkObject($entity);
  112. $this->getObjectManager()->remove($entity);
  113. if ($andFlush) {
  114. $this->getObjectManager()->flush();
  115. }
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function getTableName()
  121. {
  122. return $this->getObjectManager()->getClassMetadata($this->class)->table['name'];
  123. }
  124. /**
  125. * Returns the related Object Repository.
  126. *
  127. * @return ObjectRepository
  128. */
  129. protected function getRepository()
  130. {
  131. return $this->getObjectManager()->getRepository($this->class);
  132. }
  133. /**
  134. * @param $object
  135. *
  136. * @throws \InvalidArgumentException
  137. */
  138. protected function checkObject($object)
  139. {
  140. if (!$object instanceof $this->class) {
  141. throw new \InvalidArgumentException(sprintf(
  142. 'Object must be instance of %s, %s given',
  143. $this->class, is_object($object) ? get_class($object) : gettype($object)
  144. ));
  145. }
  146. }
  147. }