Skip to content

Instantly share code, notes, and snippets.

@lutangar
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lutangar/d1203122f815ba0c0898 to your computer and use it in GitHub Desktop.
Save lutangar/d1203122f815ba0c0898 to your computer and use it in GitHub Desktop.
BaseManager
<?php
namespace GeorgetteParty\Manager\BaseManager;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityNotFoundException;
/**
* Class BaseManager
*
* @package GeorgetteParty\BaseBundle\Manager
* @author Johan Dufour <johan.dufour@gmail.com>
*/
class BaseManager
{
/**
* Entity name
*
* @var string
*/
protected $entityName;
/**
* Entity manager $entityManager
*
* @var \Doctrine\Common\Persistence\ObjectManager
*/
protected $entityManager;
/**
* Construct a manager with its entityManager
*
* @param string $entityName
* @param ObjectManager $entityManager
*/
public function __construct($entityName, ObjectManager $entityManager)
{
$this->entityName = $entityName;
$this->entityManager = $entityManager;
}
/**
* Save an object
*
* @param $object
* @param bool $andFlush
* @return BaseManager
*/
public function save($object, $andFlush = true)
{
$entityManager = $this->getEntityManager();
$entityManager->persist($object);
if ($andFlush) {
$entityManager->flush();
}
return $this;
}
/**
* Delete an entity
*
* @param $object
* @param bool $andFlush
* @return BaseManager
*/
public function delete($object, $andFlush = true)
{
$this->getEntityManager()->remove($object);
if ($andFlush) {
$this->getEntityManager()->flush();
}
return $this;
}
/**
* Delete a collection of entities
*
* @param array $collection
* @param bool $andFlush
*/
public function deleteCollection($collection, $andFlush = true)
{
foreach ($collection as $item) {
$this->getEntityManager()->remove($object);
}
if ($andFlush) {
$this->getEntityManager()->flush();
}
}
/**
* Delete an entity by its id
*
* @param $id
* @param bool $andFlush
* @return BaseManager
*
* @throws EntityNotFoundException
*/
public function deleteById($id, $andFlush = true)
{
$object = $this->find($id);
if (!$object) {
throw new EntityNotFoundException();
}
$this->getEntityManager()->remove($object);
if ($andFlush) {
$this->getEntityManager()->flush();
}
return $this;
}
/**
* Find all entities from the current repository
*
* @return array
*/
public function findAll()
{
return $this->getRepository()->findAll();
}
/**
* Find one entity by its id
*
* @param $id
* @return mixed
*/
public function find($id)
{
return $this->getRepository()->find($id);
}
/**
* Shortcut method to flush the entity manager
*/
public function flush()
{
$this->getEntityManager()->flush();
}
/**
* Return the repository object
*
* @return \Doctrine\Common\Persistence\ObjectRepository
*/
protected function getRepository()
{
return $this->getEntityManager()->getRepository($this->entityName);
}
/**
* Return the entity name
*
* @return \Doctrine\ORM\EntityManager
*/
public function getEntityName()
{
return $this->entityName;
}
/**
* Return current entityManager
*
* @return \Doctrine\ORM\EntityManager
*/
public function getEntityManager()
{
return $this->entityManager;
}
/**
* Gets the name of the single id field. Note that this only works on
* entity classes that have a single-field pk.
*
* @return string
*
* @throws \Doctrine\ORM\Mapping\MappingException If the class has a composite primary key.
*/
public function getSingleIdentifierFieldName()
{
$meta = $this->getEntityManager()->getClassMetadata($this->getEntityName());
return $meta->getSingleIdentifierFieldName();
}
}
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<!-- entities names -->
<parameter key="acme_demo.entity.recipe.class">Acme\DemoBundle\Entity\Recipe</parameter>
<parameter key="acme_demo.entity.ingredient.class">Acme\DemoBundle\Entity\Ingredient</parameter>
<!-- managers -->
<parameter key="georgette_party.base_manager.class">Acme\GeorgettePartyBundle\Manager\BaseManager</parameter>
<parameter key="acme_demo.manager.ingredient.class">Acme\DemoBundle\Manager\IngredientManager</parameter>
</parameters>
<services>
<!-- managers -->
<!-- generic implementation is enough -->
<service id="acme_demo.manager.recipe" class="%georgette_party.base_manager.class%">
<argument>%acme_demo.entity.recipe.class%</argument>
<argument type="service" id="doctrine.orm.entity_manager" />
</service>
<!-- need a specific implementation with a specifi manager class -->
<service id="acme_demo.manager.ingredient" class="%acme_demo.manager.ingredient_manager.class%">
<argument>%acme_demo.entity.recipe.class%</argument>
<argument type="service" id="doctrine.orm.entity_manager" />
</service>
</services>
</container>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment