您好,首先创建您的经理,在我的示例中,我为 CoreBundle 中的 Item 类创建了经理:
<?php
// src/Sybio/Bundle/CoreBundle/Manager/ItemManager.php:
namespace Sybio\Bundle\CoreBundle\Manager;
use Sybio\Bundle\CoreBundle\Entity\Item;
class ItemManager
{
/**
* @var \Doctrine\ORM\EntityManager $em entity manager
*/
protected $em;
/**
* @var \Doctrine\ORM\EntityRepository $em repository
*/
protected $repository;
/**
* @var string $entityName
*/
protected $entityName;
/**
* Constructor
*
* @param EntityManager $em
* @param string $entityName
*
* @return void
*/
public function __construct(EntityManager $em, $entityName)
{
$this->em = $em;
$this->repository = $em->getRepository($entityName);
$this->entityName = $entityName;
}
/**
* Save a entity object
*
* @param Object $entity
*
* @return Object Entity
*/
public function save($entity)
{
$this->persistAndFlush($entity);
return $entity;
}
/**
* Remove a entity object
*
* @param Object $entity
*
* @return Object Entity
*/
public function remove($entity)
{
return $this->removeAndFlush($entity);
}
/**
* Persist object
*
* @param mixed $entity
*
* @return void
*/
protected function persistAndFlush($entity)
{
$this->em->persist($entity);
$this->em->flush();
}
/**
* Remove object
*
* @param mixed $entity entity to remove
*
* @return void
*/
protected function removeAndFlush($entity)
{
$this->em->remove($entity);
$this->em->flush();
}
/**
* Returns entity repository object
*
* @return EntityRepository
*/
public function getRepository()
{
return $this->repository;
}
/**
* Create a new object
*
* @return mixed
*/
public function createNewObject()
{
return new Item();
}
// Create your own methods to manage the object
}
如果管理器结构在多个管理器之间共享,您可以创建一个由所有其他管理器扩展的 BaseManager !
然后在你的 bundle 的 services.yml(或 xml)文件中注册它:
# src/Sybio/Bundle/CoreBundle/Resources/config/services.yml or xml !:
parameters:
# Managers _________________
sybio.item_manager.entity: SybioCoreBundle:Item
sybio.item_manager.class: Sybio\Bundle\CoreBundle\Manager\ItemManager
services:
# Managers _________________
sybio.item_manager:
class: %sybio.item_manager.class%
arguments: [@doctrine.orm.entity_manager, %sybio.item_manager.entity%]
就是这样,你现在可以使用它了:
// Controller:
$im = $this->get('sybio.item_manager');
$item = $im->createNewObject();
$im->save($item);
然后你可以改进你的经理,这里我给我的经理一组配置参数:
# src/Sybio/Bundle/CoreBundle/Resources/config/services.yml or xml !:
sybio.item_manager:
class: %sybio.item_manager.class%
arguments: [@doctrine.orm.entity_manager, %sybio.item_manager.entity%, {'item_removed_state': %item_removed_state%, 'item_unpublished_state': %item_unpublished_state%, 'item_published_state': %item_published_state%}]
// src/Sybio/Bundle/CoreBundle/Manager/ItemManager.php:
public function __construct(EntityManager $em, $entityName, $params = array()) {
// ...
$this->params = $params;
}
如果你创建一个 BaseManager,你还可以创建一个有用的泛型方法来初始化一个对象:
// src/Sybio/Bundle/CoreBundle/Manager/BaseManager.php:
/**
* Create a new object
*
* @return mixed
*/
public function createNewObject()
{
$entityName = explode(":", $this->entityName);
$entityName = "Sybio\Bundle\CoreBundle\Entity\\".$entityName[1];
return new $entityName;
}