2013-03-13 78 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