-
-
Save NicolasBadey/7995644 to your computer and use it in GitHub Desktop.
AbstractManager for IIM
This file contains 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 IIM\ArticleBundle\Manager; | |
use Doctrine\Common\Persistence\ObjectManager; | |
abstract class Manager | |
{ | |
protected $objectManager; | |
protected $class; | |
protected $repository; | |
/** | |
* Constructor. | |
* | |
* @param ObjectManager $om | |
* @param string $class | |
*/ | |
public function __construct(ObjectManager $om, $class) | |
{ | |
$this->objectManager = $om; | |
$this->repository = $om->getRepository($class); | |
$metadata = $om->getClassMetadata($class); | |
$this->class = $metadata->getName(); | |
} | |
public function getClass() | |
{ | |
return $this->class; | |
} | |
public function getMetadata() | |
{ | |
return $this->metadata; | |
} | |
public function refresh($entity) | |
{ | |
$this->objectManager->refresh($entity); | |
} | |
public function create() | |
{ | |
$class = $this->getClass(); | |
return new $class; | |
} | |
public function update($entity, $flush = true) | |
{ | |
$this->objectManager->persist($entity); | |
if ($flush) { | |
$this->objectManager->flush(); | |
} | |
} | |
public function delete($entity, $flush = true) | |
{ | |
$this->objectManager->remove($entity); | |
if ($flush) { | |
$this->objectManager->flush(); | |
} | |
} | |
public function findOneBy(array $criteria,$exception = false) | |
{ | |
$entity = $this->repository->findOneBy($criteria); | |
if ($exception && !$entity) { | |
throw new NotFoundHttpException('Unable to find entity.'); | |
} | |
return $entity; | |
} | |
public function findBy(array $criteria) | |
{ | |
return $this->repository->findBy($criteria); | |
} | |
public function find($id,$exception = false) | |
{ | |
$entity = $this->repository->find($id); | |
if ($exception && !$entity) { | |
throw new NotFoundHttpException('Unable to find entity.'); | |
} | |
return $entity; | |
} | |
public function findAll() | |
{ | |
return $this->repository->findAll(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment