2016-12-31 4 views
-4

私はFOSUserBundleを使用しています。symfony、mysql、fosuserbundle:関連マッピング - OneToOne

私はこのUSERとADDRESSエンティティを持ち、1対1で関連しています。 ユーザーが作成されていて、アドレスを追加したいとします。 EDIT PROFILEページでは、ユーザーが住所を追加できるフォームが必要です。

ここに質問があります。私が作成しているこの新しいアドレスが、現在ログインしているユーザー用であることをAddresエンティティに知らせるにはどうすればよいですか?

これは私の編集アクションです:

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

これにより、ユーザは、エンティティ

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

を見て:

いずれかの方法は、野生の推測では、小枝にこのようなものです

関連する問題