能夠使用原則加快了很多東西但是感覺有點笨重給我不必設置/使用實體管理器在我所有的控制器。我寧願在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();
}
它是什麼/不是實體的責任負載本身或者它的兄弟姐妹。此外,您在此假設實體保持某種參照對象管理器/數據庫連接的/ etc(一種-什麼,我們用活動記錄做)。我們搬離這個模式走,特別是因爲它給人們帶來了業務邏輯轉移到實體,使得實體層泥的大球。 – Ocramius 2013-03-13 20:35:49
我100%理解並同意它沒問題。然而,我看不到如何做的是創建一個額外的業務邏輯模塊,將能夠從數據庫中獲取/查找並插入到數據庫中。這樣實體就像一個實體一樣。我有一個偷偷摸摸的疑惑,我已經把自己弄糊塗了,看不到樹林... – 2013-03-13 20:39:50
你有3個元素:'Entity' | ObjectManager(實體管理器)和ObjectRepository(實體存儲庫),其功能分別是:作爲數據包|保存對數據的更改|檢索數據。這就是全部 – Ocramius 2013-03-13 20:52:40