我有一个 3 层的表格。
内置验证不会通过所有 3 个级别触发。因此,我必须手动检查第三级的错误。如果我发现错误,则不会保留有错误的实体。刷新总是在循环结束时触发。不幸的是,即使没有被持久化,一些无效的实体也会保存在数据库中。这是我的控制器:
$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())
));
}
}
永远不会触发持久操作,但无效的 $bet 实体仍保存在数据库中。