Symfony,mysql,fosuserbundle:关联映射 - OneToOne

时间:2016-12-31 01:51:45

标签: php mysql symfony fosuserbundle

我正在使用FOSUserBundle。

所以我有这个USER和ADDRESS实体并且是一对一关联的。 我创建了用户,我想添加一个地址。 在EDIT PROFILE页面中,我想要一个用户可以添加地址的表单。

问题在于:如何让Addres实体知道我正在创建的这个新地址是针对现在登录的用户?

这是我的编辑操作:

public function editAction(Request $request)
{
    $user = $this->getUser();
    if (!is_object($user) || !$user instanceof UserInterface) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }

    /** @var $dispatcher EventDispatcherInterface */
    $dispatcher = $this->get('event_dispatcher');

    $event = new GetResponseUserEvent($user, $request);
    $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);

    if (null !== $event->getResponse()) {
        return $event->getResponse();
    }

    /** @var $formFactory FactoryInterface */
    $formFactory = $this->get('fos_user.profile.form.factory');

    $form = $formFactory->createForm();
    $form->setData($user);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        /** @var $userManager UserManagerInterface */
        $userManager = $this->get('fos_user.user_manager');

        $event = new FormEvent($form, $request);
        $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);

        $userManager->updateUser($user);

        if (null === $response = $event->getResponse()) {
            $url = $this->generateUrl('fos_user_profile_show');
            $response = new RedirectResponse($url);
        }

        $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

        return $response;
    }

    return $this->render('AppBundle:main:profile_edit.html.twig', array(
        'form' => $form->createView(),
    ));
}

这是用户实体(它包含所有设置者和获取者):

class User extends BaseUser
{
/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank()
 * @Assert\Length(min=2)
 */
protected $firstName;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank()
 * @Assert\Length(min=2)
 */
protected $lastName;

/**
 * @ORM\Column(name="is_active", type="boolean")
 */
protected $isActive;

/**
 * @ORM\OneToOne(targetEntity="Phaddress", mappedBy="user")
 */
protected $phaddress;

这是地址实体(phaddres):

class Phaddress
{
/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(name="address_first")
 */
protected $addressfirst;

/**
 * @ORM\Column(name="address_second")
 */
protected $address_second;

/**
 * @ORM\Column(name="city")
 */
protected $city;

/**
 * @ORM\Column(name="state")
 */
protected $state;

/**
 * @Assert\Length(max = 5)
 * @ORM\Column(name="zip", type="integer")
 */
protected $zip;

/**
 * @Assert\Country();
 * @ORM\Column(name="country")
 */
protected $country;

/**
 * @Assert\Length(min = 8, max = 20, minMessage = "min_lenght", maxMessage = "max_lenght")
 * @Assert\Regex(pattern="/^\(0\)[0-9]*$", message="number_only")
 * @ORM\Column(name="phone_cel")
 */
protected $phone_cel;

/**
 * @Assert\Length(min = 8, max = 20, minMessage = "min_lenght", maxMessage = "max_lenght")
 * @Assert\Regex(pattern="/^\(0\)[0-9]*$", message="number_only")
 * @ORM\Column(name="phone_nb")
 */
protected $phone_nb;

/**
 * One Cart has One Customer.
 * @ORM\OneToOne(targetEntity="User", inversedBy="phaddress")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
protected $user;

这是.twig文件中的表单:

 {{ form_start(form, { 'action': path('fos_user_profile_edit'), 'attr': { 'class': 'form-group' } }) }}
   {{ form_row(form.username, { 'attr': { 'class': 'form-control', 'style': 'margin-bottom: 0.7em' } } ) }}
   {{ form_row(form.firstName, { 'attr': { 'class': 'form-control', 'style': 'margin-bottom: 0.7em' } } ) }}
   {{ form_row(form.lastName, { 'attr': { 'class': 'form-control', 'style': 'margin-bottom: 0.7em' } } ) }}
   {{ form_row(form.email, { 'attr': { 'class': 'form-control', 'style': 'margin-bottom: 0.7em' } } ) }}
   {{ form_row(form.current_password, { 'attr': { 'class': 'form-control', 'style': 'margin-bottom: 0.7em' } } ) }}
   {{ form_row(form.phaddress.zip, { 'attr': { 'class': 'form-control', 'style': 'margin-bottom: 0.7em' } } ) }}
   <input type="submit" value="Update Profile" class="btn btn-success" />
 {{ form_end(form) }}

1 个答案:

答案 0 :(得分:0)

根本没有代码没有帮助......我们不知道你如何展示你的表格......

为什么不编辑用户实体来保存用户地址?

无论哪种方式,一个狂野的猜测就像这样在树枝上:

{{ render(controller('AppBundle:Entity:edit', { 'userId': user.id })) }}

将您的控制器editAction()修改为editAction($userId),然后查看Form Events