Skip to content

Instantly share code, notes, and snippets.

@merk
Created March 22, 2012 02:37
Show Gist options
  • Save merk/2155260 to your computer and use it in GitHub Desktop.
Save merk/2155260 to your computer and use it in GitHub Desktop.
ObjectManager/Manager service trait test. Allows multiple object Manager classes to use Doctrine's ObjectManager, rather than reimplementing the same functions again and again.
<?php
namespace Ibms\CustomerBundle\Entity;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\QueryBuilder;
/**
* Manages Group objects.
*
* @author Tim Nagel <[email protected]>
*/
class GroupManager
{
/**
* Imports the base Manager trait allowing use of the ObjectManager provided
* by doctrine.
*/
use \Infinite\Helper\ManagerTrait;
/**
* Manager constructor.
*
* @param \Doctrine\Common\Persistence\ObjectManager $objectManager
* @param string $class
*/
public function __construct(ObjectManager $objectManager, $class)
{
$this->setObjectManager($objectManager, $class);
}
/**
* Overrides the trait's doQueryAll method to provide sorting
* by the appropriate field.
*
* @param \Doctrine\ORM\QueryBuilder $qb
* @param $alias
*/
protected function doQueryAll(QueryBuilder $qb, $alias)
{
$qb->orderBy(sprintf('%s.name', $alias));
}
/**
* Saves a customer, optionally flushing the ObjectManager to cause a
* write to occur.
*
* @param Customer $customer
* @param Boolean $flush
*/
public function save(Group $group, $flush = true)
{
$this->doSave($group, $flush);
}
}
<?php
namespace Infinite\Helper;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
/**
* Contains a grouping of functions related to Manager services and
* their use of the Doctrine ObjectManager.
*
* Provides basic functions like
* - queryAll
* - find
* - findBy
* - findOneBy
*
* @author Tim Nagel <[email protected]>
*/
trait ManagerTrait
{
/**
* Persistence Manager.
*
* @var ObjectManager|EntityManager
*/
private $objectManager;
/**
* Repository for the Object.
*
* @var ObjectRepository|EntityRepository
*/
private $objectRepository;
/**
* @var \Doctrine\ORM\Mapping\ClassMetadata
*/
private $objectMetadata;
/**
* Sets the ObjectManager up. Must be called after the implementing
* class has getObjectClass set up.
*
* @param \Doctrine\Common\Persistence\ObjectManager $objectManager
* @param string $class
* @throws \LogicException when getObjectClass
*/
protected function setObjectManager(ObjectManager $objectManager, $class)
{
$this->objectManager = $objectManager;
$this->objectMetadata = $objectManager->getClassMetadata($class);
$this->objectRepository = $objectManager->getRepository($class);
}
/**
* Retrieves the object manager.
*
* @return ObjectManager|EntityManager
* @throws \LogicException when setObjectManager has not been called.
*/
protected function getObjectManager()
{
if (!$this->objectManager) {
throw new \LogicException(sprintf('Called %s#getObjectManager when %s#setObjectManager has not been called.', __CLASS__, __TRAIT__));
}
return $this->objectManager;
}
/**
* Retrieves the object repository for this manager.
*
* @return ObjectRepository|EntityRepository
* @throws \LogicException when setObjectManager has not been called.
*/
protected function getObjectRepository()
{
if (!$this->objectRepository) {
throw new \LogicException(sprintf('Called %s#getObjectRepository when %s#setObjectManager has not been called.', __CLASS__, __TRAIT__));
}
return $this->objectRepository;
}
/**
* Retrieves metadata for the object.
*
* @return \Doctrine\ORM\Mapping\ClassMetadata
* @throws \LogicException
*/
protected function getObjectMetadata()
{
if (!$this->objectMetadata) {
throw new \LogicException(sprintf('Called %s#getObjectMetadata when %s#setObjectManager has not been called.', __CLASS__, __TRAIT__));
}
return $this->objectMetadata;
}
/**
* Configures the query builder. Override in the implementing class
* to specify conditions for queryAll.
*
* @param \Doctrine\ORM\QueryBuilder $qb
* @param string $alias
* @return \Doctrine\ORM\QueryBuilder
*/
protected function doQueryAll(QueryBuilder $qb, $alias)
{
return $qb;
}
/**
* Creates a new object that can be consumed by this manager.
*
* @return object
*/
public function create()
{
$class = $this->objectRepository->getClassName();
return new $class;
}
/**
* Finds an object by its identifier.
*
* @param mixed $id The identifier or array of identifiers
* @return object
*/
public function find($id)
{
return $this->objectRepository->find($id);
}
/**
* Finds objects by the specified criteria.
*
* @param array $criteria
* @param array|null $orderBy
* @param null $limit
* @param null $offset
* @return array|mixed
*/
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
return $this->objectRepository->findBy($criteria, $orderBy, $limit, $offset);
}
/**
* Finds one object by the specified criteria.
*
* @param array $criteria
* @return object
*/
public function findOneBy(array $criteria)
{
return $this->objectRepository->findOneBy($criteria);
}
/**
* Returns a QueryBuilder object to query for all objects.
*
* @param string $alias
*/
public function queryAll($alias = 'e')
{
$qb = $this->objectRepository->createQueryBuilder($alias);
$this->doQueryAll($qb, $alias);
return $qb;
}
/**
* Checks if the object can be handled by this Manager.
*
* @param $object
* @return bool
*/
public function handles($object)
{
$class = $this->objectRepository->getClassName();
return $object instanceof $class;
}
/**
* Checks if the object is known to the ObjectManager
*
* @return bool
*/
public function isNew($object)
{
return $this->handles($object) && !$this->objectManager->getUnitOfWork()->isInIdentityMap($object);
}
/**
* Persists and optionally flushes an object managed by this
* manager.
*
* To use this method the object implementing this trait
* should create a save() method that calls this method.
*
* @param object $object
*/
protected function doSave($object)
{
$this->objectManager->persist($object);
$this->objectManager->flush();
}
/**
* Tells the OM about an object that has come from a serialization.
*
* @param $object
*/
public function merge($object)
{
$this->objectManager->merge($object);
}
/**
* Begins a transaction to wrap operations.
*/
protected function begin()
{
$this->objectManager->getConnection()->beginTransaction();
}
/**
* Commits a transaction.
*/
protected function commit()
{
$this->objectManager->getConnection()->commit();
}
/**
* Rolls back a transaction.
*/
protected function rollback()
{
$this->objectManager->getConnection()->rollback();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment