我有3行要插入同一个表中,该表将从一个查询中一次插入。这就是我的尝试:
控制器功能:
public function addAction(Request $request)
{
$entity = new GlobalSetting();
$minPLabel = $_POST['minPlayer'];
$minPValue = $_POST['minPlayerValue'];
$maxPLabel = $_POST['maxPlayer'];
$maxPValue = $_POST['maxPlayerValue'];
$overLabel = $_POST['overs'];
$overValue = $_POST['oversValue'];
if ($request->getMethod() == 'POST')
{
// echo "<pre>"; print_r($_POST); exit;
$entity->setKeyName($minPLabel);
$entity->setValue($minPValue);
$entity->setKeyName($maxPLabel);
$entity->setValue($maxPValue);
$entity->setKeyName($overLabel);
$entity->setValue($overValue);
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('global'));
}
return $this->redirect($this->generateUrl('global'));
}
这只是插入最后一条记录,我知道有些循环会去那里,但没有得到如何执行它。请帮助我被困。感谢名单
答案 0 :(得分:3)
我建议您使用如下
$minEntity = new GlobalSetting();
$minEntity->setKeyName($minPLabel);
$minEntity->setValue($minPValue);
$maxEntity = new GlobalSetting();
$maxEntity->setKeyName($maxPLabel);
$maxEntity->setValue($maxPValue);
$overEntity = new GlobalSetting();
$overEntity->setKeyName($overLabel);
$overEntity->setValue($overValue);
$em = $this->getDoctrine()->getManager();
$em->persist($minEntity);
$em->persist($maxEntity);
$em->persist($overEntity);
$em->flush();