[DoctrineBundle, WebBundle] Enhancing API for retrieving database connections and entity managers from controllers to allow for specifying the name of the connection or entity manager. If no argument is given it returns the default configured connection or entity manager.
This commit is contained in:
parent
2db073b03a
commit
6ead924835
@ -16,25 +16,65 @@ use Symfony\Components\RequestHandler\Exception\NotFoundHttpException;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Doctrine ORM controller gives you access to entity managers and DQL queries.
|
||||||
*
|
*
|
||||||
* @package symfony
|
* @package symfony
|
||||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||||
|
* @author Jonathan H. Wage <jonwage@gmail.com>
|
||||||
*/
|
*/
|
||||||
class DoctrineController extends Controller
|
class DoctrineController extends Controller
|
||||||
{
|
{
|
||||||
protected function getEntityManager()
|
public function getDatabaseConnection($name = null)
|
||||||
{
|
{
|
||||||
return $this->container->getDoctrine_ORM_EntityManagerService();
|
if ($name)
|
||||||
|
{
|
||||||
|
return $this->container->getService(sprintf('doctrine.dbal.%s_connection', $name));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return $this->container->getDatabaseConnectionService();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createQueryBuilder()
|
/**
|
||||||
|
* Get the default entity manager service or the entity manager
|
||||||
|
* with the given name.
|
||||||
|
*
|
||||||
|
* @param string $name Optional entity manager service name
|
||||||
|
* @return object $em
|
||||||
|
*/
|
||||||
|
protected function getEntityManager($name = null)
|
||||||
{
|
{
|
||||||
return $this->getEntityManager()->createQueryBuilder();
|
if ($name)
|
||||||
|
{
|
||||||
|
return $this->container->getService(sprintf('doctrine.orm.%s_entity_manager', $name));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return $this->container->getDoctrine_ORM_EntityManagerService();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createQuery($dql = '')
|
/**
|
||||||
|
* Create a new QueryBuilder instance.
|
||||||
|
*
|
||||||
|
* @param string $name Optional entity manager service name
|
||||||
|
* @return object QueryBuilder
|
||||||
|
*/
|
||||||
|
public function createQueryBuilder($name = null)
|
||||||
{
|
{
|
||||||
return $this->getEntityManager()->createQuery($dql);
|
return $this->getEntityManager($name)->createQueryBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Query instance.
|
||||||
|
*
|
||||||
|
* @param string $dql Optional Dql string to create the query from
|
||||||
|
* @param string $name Optional entity manager service name
|
||||||
|
* @return object QueryBuilder
|
||||||
|
*/
|
||||||
|
public function createQuery($dql = '', $name = null)
|
||||||
|
{
|
||||||
|
return $this->getEntityManager($name)->createQuery($dql);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ use Symfony\Components\DependencyInjection\ContainerInterface;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* WebBundle Controller gives you convenient access to all commonly needed services.
|
||||||
*
|
*
|
||||||
* @package symfony
|
* @package symfony
|
||||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||||
@ -38,11 +38,6 @@ class Controller
|
|||||||
return $this->container->getUserService();
|
return $this->container->getUserService();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDatabaseConnection()
|
|
||||||
{
|
|
||||||
return $this->container->getDatabaseConnectionService();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getMailer()
|
public function getMailer()
|
||||||
{
|
{
|
||||||
return $this->container->getMailerService();
|
return $this->container->getMailerService();
|
||||||
|
Reference in New Issue
Block a user