Skip to content

Instantly share code, notes, and snippets.

@NicolasBadey
Last active December 31, 2015 13:49
Show Gist options
  • Save NicolasBadey/7995644 to your computer and use it in GitHub Desktop.
Save NicolasBadey/7995644 to your computer and use it in GitHub Desktop.
AbstractManager for IIM
<?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