Created
May 19, 2016 00:20
-
-
Save ybouhjira/463d87aabddb7436217e711e500f475b to your computer and use it in GitHub Desktop.
Doctrine shortcuts
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 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