2013-03-13 74 views
2

能够使用原则加快了很多东西但是感觉有点笨重给我不必设置/使用实体管理器在我所有的控制器。我宁愿在1个特定的模块中拥有所有的数据库逻辑。也许我只是想着这个错误的方式,而有人可以指引我走向正确的方向。ZF2主义实体的findAll

目前,我有我的实体,功能就好了,我可以做插入数据库细跟以下

namespace Manage\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 


class ViewController extends AbstractActionController { 
    public function somethingAction(){ 
     $objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); 
     $user = new \Manage\Entity\User(); 
     $user->setname('foo'); 
     $user->settitle('bar');   
     $objectManager->persist($user); 
     $objectManager->flush(); 
    } 
} 

然而,每当我想选择我必须确保到数据库东西添加

use Doctrine\ORM\EntityManager; 

然后控制器功能如下列表...

/** 
* @var EntityManager 
*/ 
protected $entityManager; 

/** 
* Sets the EntityManager 
* 
* @param EntityManager $em 
* @access protected 
* @return PostController 
*/ 
protected function setEntityManager(EntityManager $em) { 
    $this->entityManager = $em; 
    return $this; 
} 

/** 
* Returns the EntityManager 
* 
* Fetches the EntityManager from ServiceLocator if it has not been initiated 
* and then returns it 
* 
* @access protected 
* @return EntityManager 
*/ 
protected function getEntityManager() { 
    if (null === $this->entityManager) { 
     $this->setEntityManager($this->getServiceLocator()->get('Doctrine\ORM\EntityManager')); 
    } 
    return $this->entityManager; 
} 

一旦我添加了所有的,现在我可以做一个查询我的getsomethingAction是这样的...

public function getsomethingAction() { 
    $repository = $this->getEntityManager()->getRepository('Manage\Entity\User'); 
    $list = $repository->findAll(); 
    var_dump($list); 
    return new ViewModel(); 
} 

对我来说,感觉很笨重......我可以做一个插入,而不需要所有的额外功能,但我不能做一个选择?是否有可能延长实体类,以获得由调用$库= $这个 - > getEntityManager()提供的查找/的findAll等功能 - > getRepository(“管理\实体\用户”);直接在实体内部?

我的意思是我希望能够直接运行在实体发现,我会当我设置数据...象下面这样:

public function getsomethingAction(){ 
    $list = new \Manage\Entity\User(); 
    $l = $list->findAll(); 
    var_dump($l); 
    return new ViewModel(); 
} 
+0

它是什么/不是实体的责任负载本身或者它的兄弟姐妹。此外,您在此假设实体保持某种参照对象管理器/数据库连接的/ etc(一种-什么,我们用活动记录做)。我们搬离这个模式走,特别是因为它给人们带来了业务逻辑转移到实体,使得实体层泥的大球。 – Ocramius 2013-03-13 20:35:49

+0

我100%理解并同意它没问题。然而,我看不到如何做的是创建一个额外的业务逻辑模块,将能够从数据库中获取/查找并插入到数据库中。这样实体就像一个实体一样。我有一个偷偷摸摸的疑惑,我已经把自己弄糊涂了,看不到树林... – 2013-03-13 20:39:50

+0

你有3个元素:'Entity' | ObjectManager(实体管理器)和ObjectRepository(实体存储库),其功能分别是:作为数据包|保存对数据的更改|检索数据。这就是全部 – Ocramius 2013-03-13 20:52:40

回答

3

好了,所以我的主要目标至今已经将复杂逻辑从控制器中移出到可重复使用的模型中。所以用这个例子回答我创建一个接口,它的复杂逻辑将然而生活也让我仍然使用该模型在控制器从数据库中获取数据...这里是示范...

namespace Manage\Model; 

use Doctrine\ORM\EntityManager; 

class ApiInterface { 

    /** 
    * @var EntityManager 
    */ 
    protected $entityManager; 
    protected $sl; 

    /** 
    * Sets the EntityManager 
    * 
    * @param EntityManager $em 
    * @access protected 
    * @return PostController 
    */ 
    protected function setEntityManager(EntityManager $em) { 
     $this->entityManager = $em; 
     return $this; 
    } 

    /** 
    * Returns the EntityManager 
    * 
    * Fetches the EntityManager from ServiceLocator if it has not been initiated 
    * and then returns it 
    * 
    * @access protected 
    * @return EntityManager 
    */ 
    protected function getEntityManager() { 
     if (null === $this->entityManager) { 
      $this->setEntityManager($this->sl->get('Doctrine\ORM\EntityManager')); 
     } 
     return $this->entityManager; 
    } 

    public function __construct($ServiceLocator) { 
     $this->sl = $ServiceLocator; 
    } 

    public function get() { 
     $repository = $this->getEntityManager()->getRepository('Manage\Entity\ApiList'); 
     return $repository; 
    } 

    public function set() { 
     return new \Manage\Entity\ApiList(); 
    } 

    public function save($data) { 
     $objectManager = $this->sl->get('Doctrine\ORM\EntityManager'); 
     $objectManager->persist($data); 
     $objectManager->flush(); 
    } 

    public function doComplexLogic($foo,$bar){ 
     // Can now use both set() and get() to inspect/modify/add data 
    } 

} 

所以现在我的控制器内,我可以做一些事情,从表中得到一些基本数据,如:

public function getapiAction() { 
    $api = new \Manage\Model\ApiInterface($this->getServiceLocator()); 
    var_dump($api->get()->findAll()); 
    return new ViewModel(); 
} 

,并迅速设置从控制器的数据我可以这样做:

public function setapiAction() { 
    $apiInterface = new \Manage\Model\ApiInterface($this->getServiceLocator()); 
    $api= $apiInterface->set(); 
    $user->setfoo('blah'); 
    $user->setbar('moo'); 
    $apiInterface->save($api); 
    return new ViewModel(); 
} 

而且这也让我通过采取复杂了,像这样的控制器的运行从控制器复杂的逻辑......

public function complexAction(){ 
    $foo = $this->params()->fromQuery(); 
    $bar = $this->params()->fromPost(); 
    $apiInterface = new \Manage\Model\ApiInterface($this->getServiceLocator()); 
    $apiInterface->doComplexLogic($foo, $bar); 
} 

请让我知道意见,如果这个答案是做事情的正确方法,我意识到这是非常简单和通用,但我想继续保持这种方式,以便其他人可以理解为什么,如果这是一个不错的方法/没有等

+0

一些初步测试(根据我的答案)的好处是我得到在我的IDE中完全自动完成,到目前为止,我没有看到任何性能损失,而是以我的问题发布的方式,而不是我原来的方式。我非常好奇你的想法@Ocramius – 2013-03-13 22:01:39