我正在尝试在创建客户端时创建多个客户端。这些角色保存在clientstructure表中,其中每个客户端的每个角色都是自己的条目。我希望在创建新客户端时创建角色。默认情况下,所有角色都归新客户所有。当我尝试将客户端ID持久化到数据库(外键)时,它会出现此错误:
使用params [“Client”,null,null]执行'INSERT INTO clientstructure(role,clientid,roleowner)VALUES(?,?,?)'时发生异常: SQLSTATE [23000]:完整性约束违规:1048列'clientid'不能为空
我的代码:
public function newAction(Request $request)
{
$client = new Client();
$form = $this->createForm('AppBundle\Form\ClientType', $client);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($client);
$roles = ['Client', 'Invoice receiver', 'Payment maker', 'Location'];
foreach ($roles as $role) {
$clientstructure = new Clientstructure();
$clientstructure->setClientid($client->getClientid());
$clientstructure->setRole($role);
$clientstructure->setRoleowner($client->getClientid());
$em->persist($clientstructure);
}
$em->flush();
return $this->redirectToRoute('client_show', array('clientid' => $client->getClientid()));
}
return $this->render('client/new.html.twig', array(
'client' => $client,
'form' => $form->createView(),
));
}