duandong1869 2013-01-03 11:38
浏览 30
已采纳

在表单提交后保存实体而不保留

I have a Form with 3 Layers.

The built in validation is somehow not triggered through all 3 levels. Therefore I have to check manually for Errors on the third level. If I find an error, the entity with the error is not persisted. A flush is always triggered at the end of the loop. Unfortunately even without being persisted, some invalid entities are saved in the database. Here is my Controller for it:

    $form = $this->createForm(new GameListType(), $betRound);

    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {
        $form->bind($request);
        $betRound = $form->getData();
        if ($form->isValid()) {
            foreach ($betRound->getGames() as $game) {
                if ($game->hasBet()) {
                    $bet = $game->getBet();
                    // Filter are used during select
                    // , but dont work for inserts
                    // should work in this current context!
                    $bet->setGame($game);
                    $bet->setBetRound($betRound);
                    $bet->setUser($user);

                    $validator = $this->container->get('validator');
                    $errors = $validator->validate($bet);
                    if (count($errors) == 0) {
                        print($bet. ' got persisted'); <-- never triggered
                        $em->persist($bet);
                    } else {
                        // Manual Error Handling 
                        // (no cascade Validation to third level
                        foreach ($errors as $violation) {
                            $form->addError(new FormError(
                                                    $violation->getMessageTemplate(),
                                                    $violation->getMessageParameters(),
                                                    $violation->getMessagePluralization()
                                    ));
                        }
                    }
                }
            } // foreach ($betRound->getGames() as $game)
            $em->flush();

            if(!$form->hasErrors()){
                return $this->redirect($this->generateUrl('betround_show', 
                        array('id' => $betRound->getId())
                        ));
            }
        }

The persist operation is never triggered, but the invalid $bet entities are still saved in the database.

  • 写回答

1条回答 默认 最新

  • douduikai0562 2013-01-03 13:05
    关注

    Check to see if you have a cascade option on the relation between game and bet.

    Is it really persist that is the problem or is it update? Are the bets already existing and you just don't want to update your changes if the validation fails? If so, what you need to do is to undo the changes when validation fails with $em->refresh($bet);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了