Last active
August 29, 2015 13:56
-
-
Save mickaelandrieu/8972067 to your computer and use it in GitHub Desktop.
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 Foo\Bar\BazBundle\Util; | |
abstract class BaseManager | |
{ | |
protected $em; | |
protected $class; | |
abstract protected function getRepository(); | |
public function getClass() | |
{ | |
return $this->class; | |
} | |
public function getEntityManager() | |
{ | |
return $this->em; | |
} | |
public function create() | |
{ | |
$class = $this->getClass(); | |
return new $class(); | |
} | |
public function delete($entity) | |
{ | |
$this->remove($entity); | |
} | |
protected function remove($entity) | |
{ | |
$this->getEntityManager()->remove($entity); | |
$this->getEntityManager()->flush(); | |
} | |
public function findOneBy(Array $fields) | |
{ | |
return $this->getRepository()->findOneBy($fields); | |
} | |
public function findBy(Array $fields) | |
{ | |
return $this->getRepository()->findBy($fields); | |
} | |
public function find($id) | |
{ | |
return $this->getRepository()->find($id); | |
} | |
public function findAll() | |
{ | |
return $this->getRepository()->findAll(); | |
} | |
public function persist($entity) | |
{ | |
return $this->getEntityManager()->persist($entity); | |
} | |
protected function persistAndFlush($entity) | |
{ | |
$this->getEntityManager()->persist($entity); | |
$this->getEntityManager()->flush(); | |
} | |
public function flush($entity) | |
{ | |
return $this->getEntityManager()->flush($entity); | |
} | |
public function save($entity) | |
{ | |
$this->persistAndFlush($entity); | |
} | |
} | |
class ArticleManager extends BaseManager | |
{ | |
protected $em; | |
protected $class; | |
protected $eventDispatcher; | |
public function __construct(EntityManager $em, $class, EventDispatcher $eventDispatcher) | |
{ | |
$this->em = $em; | |
$this->class = $em->getClassMetaData($class); | |
$this->eventDispatcher = $eventDispatcher; | |
$this->class | |
} | |
public function getRepository() | |
{ | |
return $this->em->getRepository('Foo\Bar\BazBundle\Article'); | |
} | |
public function saveArticle(Article $article) | |
{ | |
//doing some stuff here | |
$this->save($article); | |
$this->eventDispatcher->dispatch('foo_bundle.article.saved', new Event()); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment