我正在嘗試與Symfony一起使用會話。
事實上,我想在登錄前完成表單。
出於這個原因,我想這個方案:
使用會話時保存元素Symfony
- 保存在數據庫中
在會話中不同領域
public function customMadeAction(Request $request)
{
$session = $this->container->get('session');
$user = $this->container->get('security.context')->getToken()->getUser();
$CustomMade = new CustomMade();
$form = $this->createForm(new CustomMadeType(), $CustomMade);
$form->handleRequest($request);
if ($user === 'anon.') {
if($form->isValid()) {
# 1-Save in session
var_dump($CustomMade); #I have an array with all I need
$session->set('infos', $CustomMade); #It does not save my informations
# 2-Redirect to login/register
$securityContext = $this->container->get('security.context');
if (!$securityContext->isGranted('ROLE_USER')) {
throw new AccessDeniedException('Accès refusé');
} else {
# 3-Save in database
$em = $this->getDoctrine()->getManager();
$em->persist($CustomMade);
$em->flush();
}
}
}else{
if($form->isValid()) {
$CustomMade->setIdUser($user->getId());
$em = $this->getDoctrine()->getManager();
$em->persist($CustomMade);
$em->flush();
}
}
return $this->render('FrontBundle:Forms:customMade.html.twig', array(
'form' => $form->createView()
));
}
由於這是公認的答案我想它的工作原理。你能給我一個鏈接到它提到這個自動序列化的文檔嗎?我無法找到任何。 – AntoineWDG