我在 Symfony2 和 Doctrine 2.1.6 下工作,我尝试设置一个多步骤表单。在每个表单页面之间,我尝试将学说实体发送到 $_SESSION。
根据该 dotrine 文档,甚至可以解决多页表单: http ://docs.doctrine-project.org/en/2.1/cookbook/entities-in-session.html
但是根据stackoverflow上的许多其他帖子,不可能将实体发送到会话中。
我有以下控制器动作,我几乎复制/过去了学说文档。
public function indexAction(Request $request, $id)
{
$session = $request->getSession();
$em = $this->getDoctrine()->getEntityManager();
if (isset($_SESSION['propertyAdd'])) {
$property = $_SESSION['propertyAdd'];
$property = $em->merge($property);
}
else {
$property = new property;
}
$form = $this->createForm(new propertyType($this->getDoctrine()),$property);
// check form
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()){
$em->detach($property);
$_SESSION['propertyAdd'] = $property;
// redirection to next step here
}
}
return $this->render('AddProperty:'.$id.'.html.twig', array(
'form' => $form->createView(),));
}
行 $_SESSION['propertyAdd'] = $property; 给我以下错误:
Fatal error: Uncaught exception 'ErrorException' with message 'Notice: Unknown: "id" returned as member variable from __sleep() but does not exist in Unknown line 0' in G:..\Symfony\vendor\symfony\src\Symfony\Component\HttpKernel\Debug\ErrorHandler.php on line 65
如果我使用 Symfony2 助手替换这一行
$session->set('propertyAdd', $property);
它抛出以下异常:
Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector::serialize() must return a string or NULL
学说示例是否可行。