如果您在框架包中使用控制器,并在控制器中编写持久性逻辑,则可以Symfony\Bundle\FrameworkBundle\Controller\Controller
使用以下内容进行扩展
namespace ExampleNameSpace\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class BaseController extends Controller
{
public function persistAndSave($entity)
{
$em = $this->getDoctrine()->getEntityManager();
$em->persist($entity);
$em->flush();
}
}
在所有其他控制器中,您将扩展您的ExampleNameSpace\Controller\BaseController
而不是来自 Symfony Frameworkbundle 的控制器。
或者,我采用的方法是为每个具有类名和注入的学说实体管理器的实体编写一个管理器类。每个管理器类都使用以下方法扩展一个抽象管理器类:
<?php
abstract class AbstractManager
{
/**
* The Entity Manager
*
* @var \Doctrine\ORM\EntityManager The Doctrine Entity Manager
*/
protected $em;
/**
* The full class path associated with the repository
*
* @var string
*/
protected $class;
/**
* The repository for the manager
*
* @var \Doctrine\ORM\EntityRepository
*/
protected $repository;
/**
* Creates a new instance of the primary class managed by a given
* manager
*
* @return object A new instance of the entity being managed
*/
public function create()
{
return new $this->class();
}
/**
* {@inheritDoc}
*/
public function save($object, $flush = false)
{
if( ! $this->supportsClass($object))
{
throw new \InvalidArgumentException(sprintf('Invalid entity passed to this manager, expected instance of %s', $this->class));
}
$this->em->persist($object);
if($flush === true)
{
$this->flush();
}
return $object;
}
/**
* {@inheritDoc}
*/
public function delete($object, $flush = false)
{
if( ! $this->supportsClass($object))
{
throw new \InvalidArgumentException(sprintf('Invalid entity passed to this manager, expected instance of %s', $this->class));
}
$this->em->remove($object);
if($flush === true)
{
$this->flush();
}
return true;
}
/**
* Convenience method providing access to the entity manager flush method
*/
public function flush()
{
$this->em->flush();
}
/**
* {@inheritDoc}
*/
public function supportsClass($object)
{
return $object instanceof $this->class || is_subclass_of($object, $this->class);
}
/**
* Set class. Setter for dependency injection
*
* @param object $class A class related to this manager
*/
public function setClass($class)
{
$this->class = $class;
}
/**
* Set entity manager. Setter for dependency injection
*
* @param \Doctrine\ORM\EntityManager $entity_manager
*/
public function setEntityManager(\Doctrine\ORM\EntityManager $entity_manager)
{
$this->em = $entity_manager;
}
/**
* Returns the repository
*
* @return \Doctrine\ORM\EntityRepository A Doctrine Repository for the
* class related to this Manager
*/
protected function getRepository()
{
if( ! $this->repository)
{
$this->repository = $this->em->getRepository($this->class);
}
return $this->repository;
}
}
管理器在依赖注入容器中配置,具有实体的适当类,并提供创建、保存和删除他们负责的实体的访问权限,以及访问存储库的权限。
可以在具有管理器的控制器中创建一个实体,如下所示:
public function createAction(Request $request)
{
$entityManager = $this->get('some.entity.manager');
$entity = $entityManager->create();
$form = $this->createForm(new EntityForm(), $entity);
$form->bindRequest($request);
if($form->isValid())
{
$entityManager->save($entity, true);
}
}