Php Symfony编辑会话对象
我使用symfony2构建了一个页面,登录用户可以在其中更新详细信息。为此,在profileAction下的控制器中,我使用dotorine从会话中获取用户id来获取用户Php Symfony编辑会话对象,php,forms,session,post,symfony,Php,Forms,Session,Post,Symfony,我使用symfony2构建了一个页面,登录用户可以在其中更新详细信息。为此,在profileAction下的控制器中,我使用dotorine从会话中获取用户id来获取用户 $user = $this->getUser(); $entity = $em->getRepository('GibboCodeKeeperBundle:User')->find($user->getId()); 然后我将$entity对象绑定到一个表单,但是当表单提交时,绑定的$entity对
$user = $this->getUser();
$entity = $em->getRepository('GibboCodeKeeperBundle:User')->find($user->getId());
然后我将$entity对象绑定到一个表单,但是当表单提交时,绑定的$entity对象中现在更新的所有内容也会在会话中的$user对象中更改。反之亦然,如果我刷新$user对象,$实体也会被刷新:S
我希望$entity代表从表单中返回的内容,$user代表数据库中当前的内容,然后再尝试持久化,这样我就可以看到用户更新了哪些内容。您可以尝试以下操作:
$user = $this->getUser();
$entity = $em->getRepository('GibboCodeKeeperBundle:User')->find($user->getId());
$form = $this->createForm($formType, $entity);
$form->bind($request);
if ($form->isValid()) {
// here is your updated user
$entity = $form->getData();
// this will not update uses session
$em->refresh($user);
}
或者您可以克隆实体
$oldEntity = clone $entity;
您可以尝试以下方法:
$user = $this->getUser();
$entity = $em->getRepository('GibboCodeKeeperBundle:User')->find($user->getId());
$form = $this->createForm($formType, $entity);
$form->bind($request);
if ($form->isValid()) {
// here is your updated user
$entity = $form->getData();
// this will not update uses session
$em->refresh($user);
}
或者您可以克隆实体
$oldEntity = clone $entity;
运气不好:($user和$entity仍然保存着完全相同的数据:SDon不会将请求绑定到表单,这实际上就是从表单中获取数据并将其写入实体的原因。您还可以通过执行
$form->get('username');
克隆实体对我不起作用,$em->refresh($user)when!$form->isValid)从表单中获取特定数据()做这个小把戏:($user和$entity仍然保存着完全相同的数据:SDon不会将请求绑定到表单,这实际上就是从表单中获取数据并将其写入实体的原因。您还可以通过执行$form->get('username')从表单中获取特定数据;
克隆实体对我不起作用,$em->refresh($user)当!$form->isValid()执行此操作时