Created
March 19, 2017 19:38
-
-
Save Muspi/8f8cc504aca9a8971c9ea94c948c5ade to your computer and use it in GitHub Desktop.
Base Entity Manager for Symfony Project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Manager; | |
/** | |
* Base Entity Manager with common methods commonly used | |
* | |
* @author benoit | |
*/ | |
use Doctrine\ORM\EntityManager; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use Doctrine\Common\Persistence\ObjectRepository; | |
/** | |
* Class BaseManager | |
*/ | |
abstract class BaseManager | |
{ | |
/** | |
* @var ManagerRegistry | |
*/ | |
protected $em; | |
/** | |
* @var string | |
*/ | |
protected $class; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getConnection() | |
{ | |
return $this->getEntityManager()->getConnection(); | |
} | |
/** | |
* @return EntityManager | |
*/ | |
public function getEntityManager() | |
{ | |
return $this->em; | |
} | |
/** | |
* @param string $class | |
* @param EntityManager $em | |
*/ | |
public function __construct($class, EntityManager $em) | |
{ | |
$this->em = $em; | |
$this->class = $class; | |
} | |
/** | |
* {@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->em->persist($entity); | |
if ($andFlush) { | |
$this->em->flush(); | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function delete($entity, $andFlush = true) | |
{ | |
$this->checkObject($entity); | |
$this->em->remove($entity); | |
if ($andFlush) { | |
$this->em->flush(); | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getTableName() | |
{ | |
return $this->em->getClassMetadata($this->class)->table['name']; | |
} | |
/** | |
* Returns the related Object Repository. | |
* | |
* @return ObjectRepository | |
*/ | |
public function getRepository() | |
{ | |
return $this->em->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) | |
)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment