2016-12-31 67 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