2012-02-14 36 views

回答

1

在某種程度上,它取決於你如何引導學說。結果是讓EntityManager作爲資源存儲在Bootstrap中,然後在控制器中檢索它。例如,如果您有其中您實例化實體管理器的方法Bootstrap::_initDoctrine(),則返回此實例會將其作爲資源存儲在引導程序中,作爲密鑰'doctrine'下的資源。然後,在你的控制器的init()方法,你可以有:

$this->em = $this->getInvokeArg('bootstrap')->getResource('doctrine'); 

另外,如果您使用Bisna application resource plugin for Doctrine,那麼你必須按下一個多水平得到實體管理器:

$this->em = $this->getInvokeArg('bootstrap')->getResource('doctrine')->getEntityManager(); 
+0

我一個DoctrineEntityManager資源,用於初始化Doctrine框架並創建一個EntityManager的實例。不過,我也想在Doctrine中使用ZFDebug工具,所以我不得不使用Bootstrap.php來初始化Doctrine和ZFDebug,因爲ZFDebug作爲插件或Bootstrap初始化特性需要一個EntityManager的實例 - 這是除非教條初始化,否則不可能。 Matthew Weier O'Phinney寫了一個動作助手http://mwop.net/blog/235-A-Simple-Resource-Injector-for-ZF-Action-Controllers,它允許$ this-> em在動作中使用 – Sjwdavies 2012-02-14 17:02:40