Symfony2 flush metode的返回值


Symfony2 return value of flush metode

好的,我被卡住了,文档中没有关于这方面的信息(如果我错了,请纠正我)

示例:我在控制器中更新了metod,某种形式,如果forim有效,我使用flush方法进行更改。我如何检查是否在数据库中进行了更改,以便如果更改"成功",或者如果查询没有执行,如果有错误,我可以发送闪烁消息"未能对数据库进行更改"

这是我的代码示例,但我认为flush会返回void或null,所以这不是正确的方法,也许它会在失败时返回一些我不知道的异常。。

/**
 * @Route("createpost", name="createpost")
 */
public function createPostAction(Request $request) {
    if (!$this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
        throw $this->createAccessDeniedException();
    }
    $post = new Post();
    $form = $this->createForm(new PostForm(), $post);
    $form->handleRequest($request);
    if($form->isValid()) {
        $user = $this->getUser();
        $author = $user->getUsername();
        //$post->setPublishDate(new 'DateTime);
        $post->setAuthor($author);
        $em = $this->getDoctrine()->getManager();
        $em->persist($post);
        $pom = $em->flush();
        return $this->render('success/success.html.twig', array(
           'test' => var_dump($pom)
        ));
        if($pom) {
            $this->addFlash(
                'notice',
                'You have successfully created post'
            );
        }
        return $this->redirectToRoute('home', array(), 301);
    }
    return $this->render(
        'create/post.html.twig', array(
           'form' => $form->createView()
        ));
}

你可以这样做:

...
if($form->isValid()) {
    $user = $this->getUser();
    $author = $user->getUsername();
    //$post->setPublishDate(new 'DateTime);
    $post->setAuthor($author);
    $em = $this->getDoctrine()->getManager();
    $em->persist($post);
    $em->flush();
    if(null != $post->getId()) {
        $this->addFlash(
            'notice',
            'You have successfully created post'
        );
        return $this->render('success/success.html.twig', array(
            'test' => var_dump($pom)
        ));
    }
    // This line never be called
    return $this->redirectToRoute('home', array(), 301);
}
...

然而,您不需要检查flush是否正常工作,如果出现问题,它会抛出异常。。

评论更新:

if($form->isValid()) {
    try {
        $user = $this->getUser();
        $author = $user->getUsername();
        //$post->setPublishDate(new 'DateTime);
        $post->setAuthor($author);
        $em = $this->getDoctrine()->getManager();
        $em->persist($post);
        $em->flush();
        if(null != $post->getId()) {
            $this->addFlash(
                'notice',
                'You have successfully created post'
            );
            return $this->render('success/success.html.twig', array(
                'test' => var_dump($pom)
            ));
        }
    } catch (SOMEEXCEPTION $e) {
        return $this->redirectToRoute('home', array(), 301);
    }
}

更改

    return $this->render('success/success.html.twig', array(
       'test' => var_dump($pom)
    ));
    if($pom) {
        $this->addFlash(
            'notice',
            'You have successfully created post'
        );
    }

    if($pom) {
        $this->addFlash(
            'notice',
            'You have successfully created post'
        );
    }
    return $this->render('success/success.html.twig', array(
       'test' => var_dump($pom)
    ));

在返回响应

之前,您需要添加flash