且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Symfony 3-表单模型数据丢失了属性值,这些属性值未由字段表示

更新时间:2022-12-27 21:58:34

因为仅使用secondForm数据再次提交了表单,所以您丢失了firstForm数据.

Because the form is submitted again with only the secondForm data, you are losing the firstForm data.

您可以通过3种方式保留它们:

You have 3 ways to keep them:

1)将firstForm中的数据设置到查询中

// Insert that instead of the `return $this->render` for the second form
$url = $this->generateUrl(
    $request->attributes->get('_route'),
    array_merge(
        $request->query->all(),
        array('secondForm' => true, 'name' => $workflow->getName(), 'states' => $workflow->getStates()) // change the param
    )
);
return $this->redirect($url);

$ secondFormPart = $ this-> createForm(WorkflowTransitionsType :: class,$ workflow);

name states 设置回 $ workflow 实体,在此示例中,您可以检查查询变量 secondForm 了解是否已提交第一个表格

Set back the name and states into the $workflow entity, in this example you can check the query variable secondForm to know if the first form was submitted or not

2)将具有某些隐藏字段的数据从firstForm设置到下一个PATCH请求中

您必须修改secondForm以使用一些

You have to modify the secondForm to handle the data from the firstForm with some hidden form type

3)在返回第二个表单之前在会话中设置数据

首先,您的实体将必须实现接口 Serializable 并声明方法 serialize unserialize

First of all, your entity will have to implement the interface Serializable and declare the method serialize and unserialize

喜欢

$this->get('session')->set('workflow', $workflow);

将使用方法 serialize 进行存储.

您可以使用方法 unserialize

$session = $this->get('session');
$workflow = new Workflow();
$workflow->unserialize($session->get('workflow'));

由于要将整个实体存储到会话中,因此该解决方案将大大降低应用程序的性能

Because you are storing the whole entity into the session, this solution will decrease a lot the performance of your application