123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\CoreBundle\Model;
- use Doctrine\Common\Persistence\ManagerRegistry;
- use Doctrine\Common\Persistence\ObjectManager;
- use Doctrine\Common\Persistence\ObjectRepository;
- /**
- * @author Hugo Briand <briand@ekino.com>
- */
- abstract class BaseManager implements ManagerInterface
- {
- /**
- * @var ManagerRegistry
- */
- protected $registry;
- /**
- * @var string
- */
- protected $class;
- /**
- * @param string $class
- * @param ManagerRegistry $registry
- */
- public function __construct($class, ManagerRegistry $registry)
- {
- $this->registry = $registry;
- $this->class = $class;
- }
- /**
- * @return ObjectManager
- */
- public function getObjectManager()
- {
- $manager = $this->registry->getManagerForClass($this->class);
- if (!$manager) {
- throw new \RuntimeException(sprintf(
- 'Unable to find the mapping information for the class %s.'
- .' Please check the `auto_mapping` option'
- .' (http://symfony.com/doc/current/reference/configuration/doctrine.html#configuration-overview)'
- .' or add the bundle to the `mappings` section in the doctrine configuration.',
- $this->class
- ));
- }
- return $manager;
- }
- /**
- * {@inheritdoc}
- */
- public function getClass()
- {
- return $this->class;
- }
- /**
- * {@inheritdoc}
- */
- public function findAll()
- {
- return $this->getRepository()->findAll();
- }
- /**
- * {@inheritdoc}
- */
- public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
- {
- return $this->getRepository()->findBy($criteria, $orderBy, $limit, $offset);
- }
- /**
- * {@inheritdoc}
- */
- public function findOneBy(array $criteria, array $orderBy = null)
- {
- return $this->getRepository()->findOneBy($criteria, $orderBy);
- }
- /**
- * {@inheritdoc}
- */
- public function find($id)
- {
- return $this->getRepository()->find($id);
- }
- /**
- * {@inheritdoc}
- */
- public function create()
- {
- return new $this->class();
- }
- /**
- * {@inheritdoc}
- */
- public function save($entity, $andFlush = true)
- {
- $this->checkObject($entity);
- $this->getObjectManager()->persist($entity);
- if ($andFlush) {
- $this->getObjectManager()->flush();
- }
- }
- /**
- * {@inheritdoc}
- */
- public function delete($entity, $andFlush = true)
- {
- $this->checkObject($entity);
- $this->getObjectManager()->remove($entity);
- if ($andFlush) {
- $this->getObjectManager()->flush();
- }
- }
- /**
- * {@inheritdoc}
- */
- public function getTableName()
- {
- return $this->getObjectManager()->getClassMetadata($this->class)->table['name'];
- }
- /**
- * Returns the related Object Repository.
- *
- * @return ObjectRepository
- */
- protected function getRepository()
- {
- return $this->getObjectManager()->getRepository($this->class);
- }
- /**
- * @param $object
- *
- * @throws \InvalidArgumentException
- */
- protected function checkObject($object)
- {
- if (!$object instanceof $this->class) {
- throw new \InvalidArgumentException(sprintf(
- 'Object must be instance of %s, %s given',
- $this->class, is_object($object) ? get_class($object) : gettype($object)
- ));
- }
- }
- }
|