Symfony2:如何使用表单修改当前用户的实体? [英] Symfony2: How to modify the current user's entity using a form?

查看:99
本文介绍了Symfony2:如何使用表单修改当前用户的实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加一个简单的表单来允许我的用户编辑他们的个人资料。我的问题是:

I am trying to add a simple form to allow my users to edit their profile. My problem is:

由于实体链接到表单与当前用户对象相同( $ user === $ entity ,见下文),如果表单验证失败,则视图将使用修改后的用户对象(即使用非有效形式的值)呈现。

Since the entity "linked" to the form is the same as the current user object ($user === $entity, see below), if the form validation fails, then the view is rendered with the modified user object (ie. with values of the non-valid form).

这里我的(经典)控制器:

Here my (classic) controller:

public function profileAction()
{
    $em = $this->getDoctrine()->getEntityManager();

    $user = $this->get('security.context')->getToken()->getUser();
    $entity = $em->getRepository('AcmeSecurityBundle:User')->find($user->id);
    // $user === $entity => true

    $form = $this->createForm(new ProfileType(), $entity);

    $request = $this->getRequest();

    if ($request->getMethod() === 'POST')
    {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('profile'));
        }
    }

    return $this->render('AcmeSecurityBundle:User:profile.html.twig', array(
        'entity'      => $entity,
        'form'   => $form->createView(),
    ));
}

所以我想知道如何有两个不同的对象 $用户 $ entity 。我使用 clone(),它对视图呈现部分( $ user 对象未被修改)但是它在数据库中创建了一个新的记录,而不是更新旧的记录。

So I wondered how to have two distincts objects $user and $entity. I used clone() and it worked well for the view rendering part (the $user object was not modified), but it created a new record in database instead of updating the old one.

PS:我知道我应该使用 FOSUserBundle 。但是我真的很想在这里明白我的错误:)

PS: I know I should use FOSUserBundle. But I would really like to understand my mistake here :)

推荐答案

我使用与 FOSUserBundle ,它调用 $ em-> refresh( )/ / code>表单验证失败:

I used the same solution as FOSUserBundle, which is calling $em->refresh() on my entity when the form validation fails:

public function profileAction()
{
    $em = $this->getDoctrine()->getEntityManager();

    $user = $this->get('security.context')->getToken()->getUser();
    $entity = $em->getRepository('AcmeSecurityBundle:User')->find($user->id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find User entity.');
    }

    $form = $this->createForm(new ProfileType(), $entity);

    $request = $this->getRequest();

    if ($request->getMethod() === 'POST')
    {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('profile'));
        }

        $em->refresh($user); // Add this line
    }

    return $this->render('AcmeSecurityBundle:User:profile.html.twig', array(
        'entity'      => $entity,
        'form'   => $form->createView(),
    ));
}

请注意,如果您使用所谓的 / em>in如何使用原则处理文件上传 (在我的情况下,picture_file,您需要手动清除:

Note that if you are using what is called a "virtual" field in "How to handle File Uploads with Doctrine" (in my case "picture_file" you will need to clear it by hand:

$em->refresh($user);
$user->picture_file = null; // here

这篇关于Symfony2:如何使用表单修改当前用户的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆