Created
November 16, 2012 09:15
-
-
Save manuakasam/4085757 to your computer and use it in GitHub Desktop.
Base Class for Doctrine Entity Services
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 MyNamespace\Service; | |
use Zend\EventManager\EventManagerAwareInterface; | |
use Zend\EventManager\EventManagerInterface; | |
use Zend\ServiceManager\ServiceManagerAwareInterface; | |
use Zend\ServiceManager\ServiceManager; | |
use Doctrine\ORM\EntityManager; | |
use Doctrine\ORM\EntityRepository; | |
class DoctrineEntityService implements | |
ServiceManagerAwareInterface, | |
EventManagerAwareInterface | |
{ | |
protected $serviceManager; | |
protected $eventManager; | |
protected $entityManager; | |
protected $entityRepository; | |
/** | |
* Returns all Entities | |
* | |
* @return EntityRepository | |
*/ | |
public function findAll() | |
{ | |
$entities = null; | |
$this->getEventManager()->trigger(__FUNCTION__ . '.pre', $this, array('entities' => $entities)); | |
if (null === $entities) { | |
$entities = $this->getEntityRepository()->findAll(); | |
} | |
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array('entities' => $entities)); | |
return $entities; | |
} | |
public function find($id) { | |
return $this->getEntityRepository()->find($id); | |
} | |
/** | |
* Adds a new Kennzahl into the repository | |
* | |
* @param Entity $entity | |
* @return Entity | |
*/ | |
public function add($entity) | |
{ | |
$this->getEventManager()->trigger(__FUNCTION__ . '.pre', $this, array('entity'=>$entity)); | |
$this->getEntityManager()->persist($entity); | |
$this->getEntityManager()->flush(); | |
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array('entity'=>$entity)); | |
return $entity; | |
} | |
/** | |
* @param \Doctrine\ORM\EntityRepository $entityRepository | |
* @return Referenzwert | |
*/ | |
public function setEntityRepository(EntityRepository $entityRepository) | |
{ | |
$this->entityRepository = $entityRepository; | |
return $this; | |
} | |
/** | |
* @param EntityManager $entityManager | |
* @return \Kennzahlen\Service\DoctrineEntityService | |
*/ | |
public function setEntityManager(EntityManager $entityManager) | |
{ | |
$this->entityManager = $entityManager; | |
return $this; | |
} | |
/** | |
* @return EntityManager | |
*/ | |
public function getEntityManager() | |
{ | |
return $this->entityManager; | |
} | |
/** | |
* Inject an EventManager instance | |
* | |
* @param EventManagerInterface $eventManager | |
* @return \Kennzahlen\Service\DoctrineEntityService | |
*/ | |
public function setEventManager(EventManagerInterface $eventManager) | |
{ | |
$this->eventManager = $eventManager; | |
return $this; | |
} | |
/** | |
* Retrieve the event manager | |
* Lazy-loads an EventManager instance if none registered. | |
* | |
* @return EventManagerInterface | |
*/ | |
public function getEventManager() | |
{ | |
return $this->eventManager; | |
} | |
/** | |
* Set service manager | |
* | |
* @param ServiceManager $serviceManager | |
* @return \Kennzahlen\Service\DoctrineEntityService | |
*/ | |
public function setServiceManager(ServiceManager $serviceManager) | |
{ | |
$this->serviceManager = $serviceManager; | |
return $this; | |
} | |
/** | |
* Get service manager | |
* | |
* @return ServiceManager | |
*/ | |
public function getServiceManager() | |
{ | |
return $this->serviceManager; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment