Skip to content

Instantly share code, notes, and snippets.

@ybouhjira
Created May 19, 2016 00:20
Show Gist options
  • Save ybouhjira/463d87aabddb7436217e711e500f475b to your computer and use it in GitHub Desktop.
Save ybouhjira/463d87aabddb7436217e711e500f475b to your computer and use it in GitHub Desktop.
Doctrine shortcuts
<?php
namespace AppBundle\Utils;
use ReflectionClass;
trait DoctrineTrait
{
/**
* Deduces the entity's name from the controller name
* @return string The entity's name
*/
protected function getEntityName()
{
$reflection = new ReflectionClass($this);
$className = $reflection->getShortName();
return preg_replace('/Controller$/', '', $className);
}
/**
* Returns the appropriate repository for the controller
* @return \Doctrine\Common\Persistence\ObjectRepository
*/
protected function getRepository()
{
return $this
->getDoctrine()
->getRepository('AppBundle:' . $this->getEntityName());
}
/**
* Returns doctrine's entity manager
* @return \Doctrine\Common\Persistence\ObjectManager|object
*/
protected function getManager()
{
return $this
->getDoctrine()
->getManager();
}
protected function getAllQuery()
{
return $this
->getRepository()
->createQueryBuilder('alias');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment