2017-04-02 55 views
1

如何將doctrine實體管理器傳遞給某個類構造函數?未捕獲的異常'Doctrine ORM ORMException'帶消息'EntityManager已關閉

例如,我在文件_someAction.php中設置並創建實體管理器$em。 在同一個文件中,我實例化類,並將實體管理器傳遞給構造函數,但是在類中不起作用。我得到

致命錯誤:Uncaught異常'Doctrine \ ORM \ ORMException'與消息'EntityManager已關閉'。

類dealErrCl

use Doctrine\ORM\EntityManager; 
use someNamespace\LogErr; 

class dealErrCl { 
private $em; //entity manager to save error to the entity 

public function __construct(\Doctrine\ORM\EntityManager $em) { 
     $this->em = $em; 
} 

public function flush($errArr) { 

    $this->le = new LogErr(); 

    if(isset($errArr['title'])) { 
     $this->le->setTitle($errArr['title']); 
    } 
/*...*/ 

    $this->em->persist($this->le); 
    $this->em->flush(); 

}

_someAction.php

include DbCon.php; // configures doctrine and creates $em. If i use this em in this file, it works, but if i tryt to pass it to some class constructor, it gives Fatal error. 

use LogBundle\Components\ErrC\dealErrCl; 
$dealErrIn = new dealErrCl($em); 

如果我使用_someAction.php文件這個$ EM,它的工作原理,但如果我tryt將它傳遞給一些dealErrCl構造函數,它會給出致命錯誤。

+0

似乎解決方案是在這裏:https://creativcoders.wordpress.com/tag/doctrineormexception-the-entitymanager-is-closed/ – olga

+0

我的錯誤是'desc'鍵,它是保留。不應該被用作fiedl的名字。 – olga

回答

0

這意味着在刷新過程中會出現錯誤,從而關閉實體管理器。

有可能許多可能出現的錯誤:

1)使用字段名稱保留關鍵字 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#quoting-reserved-words

POSTGRE保留字非空場

2)設置空: https://www.postgresql.org/docs/7.3/static/sql-keywords-appendix.html

MYSQL保留字: https://dev.mysql.com/doc/refman/5.7/en/keywords.html

因此,最好總是把所有的flush,放在try-catch塊內。

相關問題