Zoh*_*han 6 php symfony doctrine-orm
如何使用symfony2中的Doctrine检查记录是否已成功插入数据库?
我在控制器中的动作是
public function createAction(){
$portfolio = new PmPortfolios();
$portfolio->setPortfolioName('Umair Portfolio');
$em = $this->getDoctrine()->getEntityManager();
$em->persist($portfolio);
$em->flush();
if(){
$this->get('session')->setFlash('my_flash_key',"Record Inserted!");
}else{
$this->get('session')->setFlash('my_flash_key',"Record notInserted!");
}
}
Run Code Online (Sandbox Code Playgroud)
我应该在if声明中写些什么?
Chr*_*nel 23
您可以将控制器包装在这样的try / catch块中:
public function createAction() {
try {
$portfolio = new PmPortfolios();
$portfolio->setPortfolioName('Umair Portfolio');
$em = $this->getDoctrine()->getEntityManager();
$em->persist($portfolio);
$em->flush();
$this->get('session')->setFlash('my_flash_key',"Record Inserted!");
} catch (Exception $e) {
$this->get('session')->setFlash('my_flash_key',"Record notInserted!");
}
}
Run Code Online (Sandbox Code Playgroud)
如果插入失败,将抛出并捕获异常.您可能还希望通过调用$e->getMessage()和/或$e->getTraceAsString()解释异常来以某种方式在catch块中记录错误消息.