未捕获的例外' Doctrine \ ORM \ ORMException'消息' EntityManager已关闭

时间:2017-04-02 04:17:59

标签: doctrine-orm entitymanager

如何将doctrine实体管理器传递给某个类构造函数?

例如,我在文件_someAction.php中设置并创建实体管理器$em。 在同一个文件中,我实例化了类,并将实体管理器传递给构造函数,但是em在类中不起作用。我正在

致命错误:未捕获的异常' Doctrine \ ORM \ ORMException'消息' EntityManager已关闭。'

class 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,它可以工作,但如果我试图将它传递给某个dealErrCl构造函数,它会给出致命错误。

1 个答案:

答案 0 :(得分:0)

这意味着刷新期间出现错误,从而关闭实体管理器。

可能存在许多错误:

1)在非空字段

上设置null

2)使用保留关键字作为字段名称 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#quoting-reserved-words

POSTGRE保留字: https://www.postgresql.org/docs/7.3/static/sql-keywords-appendix.html

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

因此,最好总是在try-catch块中包含所有flush。