【发布时间】:2015-01-25 00:09:10
【问题描述】:
我有一个带有 Symfony 2 的控制台应用程序,脚本在 cron(终端)上运行。但是,在 \Doctrine\DBAL\DBALException 之后,脚本会抛出 N \Doctrine\ORM\ORMException 并带有消息“EntityManager 已关闭。”。
这是部分脚本:
try {
$this->getDoctrine()->getConnection()->beginTransaction();
// ...
$manager = $this->getDoctrine()->getManager();
$entity = new Post();
$entity
->setAuthor($author)
->setTitle($title)
->setContent($content)
;
$manager->persist($entity);
$manager->flush();
$this->getDoctrine()->getConnection()->commit();
return $entity->getId();
} catch (\Doctrine\DBAL\DBALException $e) {
$this->getDoctrine()->resetManager();
$output->writeln(sprintf(
'<error>[!] %s (%s) the post could not be created "%s"</error>',
get_class($e),
date('Y-m-d H:i:s'),
$e->getMessage()
));
return false;
} catch (\Exception $e) {
$this->getDoctrine()->getConnection()->rollback();
$output->writeln(sprintf(
'<error>[!] %s (%s) the post could not be created "%s"</error>',
get_class($e),
date('Y-m-d H:i:s'),
$e->getMessage()
));
return false;
}
如何解决?
【问题讨论】:
-
你看到这个问题和答案了吗? stackoverflow.com/a/14261250/2779152
-
是的,我在打开这个帖子之前做过。
标签: php symfony doctrine-orm doctrine