2016-12-31 64 views
-4

我正在使用FOSUserBundle。Symfony,mysql,fosuserbundle:关联映射 - OneToOne

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

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

这是我的编辑操作:

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(), 
    )); 
} 

这是用户实体(它拥有所有的getter和setter方法):

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) }} 

回答

0

任何代码都没有帮助......我们如何显示在表单上不知道...

你为什么不只是编辑您的用户实体保存用户的地址?

无论哪种方式,猜猜会是这样的树枝:

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

修改控制器editAction()editAction($userId),并期待在Form Events