2016-04-27 6 views
0

私は2つのエンティティを持っています:ManyToMany双方向関係を持つユーザーとひずみ、関係の所有者はUserです。ManyToMany双方向のEntityForm

権利を編集するためのフォーム(ユーザはいくつかの系統を所有しています)をしたいと思っています。私が望むいくつかの系統を選択できるフォームを作成すると、うまく動作します。しかし...時々、私は関係の反対側の権利を編集したい:ひずみ。ひずみを編集し、私が望むユーザーを選択します。しかし、それは、私はあなたに私のエンティティのユーザーとひずみと2にformType、そして私のUglysソリューション...

を与える...

/** 
* The authorized strains for this user. 
* 
* @var Strain|ArrayCollection 
* 
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Strain", inversedBy="authorizedUsers") 
*/ 
private $authorizedStrains; 

/** 
* User constructor. 
*/ 
public function __construct() 
{ 
    $this->authorizedStrains = new ArrayCollection(); 
} 

/** 
* Add an authorized strain. 
* 
* @param Strain $strain 
* 
* @return $this 
*/ 
public function addAuthorizedStrain(Strain $strain) 
{ 
    $this->authorizedStrains[] = $strain; 
    $strain->addAuthorizedUser($this); 

    return $this; 
} 

/** 
* Remove an authorized strain. 
* 
* @param Strain $strain 
*/ 
public function removeAuthorizedStrain(Strain $strain) 
{ 
    $this->authorizedStrains->removeElement($strain); 
    $strain->removeAuthorizedUser($this); 
} 

/** 
* Get authorized strains. 
* 
* @return Strain|ArrayCollection 
*/ 
public function getAuthorizedStrains() 
{ 
    return $this->authorizedStrains; 
} 

User.phpを

を動作しません。 Strain.php

/** 
* The authorized user. 
* For private strains only. 
* 
* @var User|ArrayCollection 
* 
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\User", mappedBy="authorizedStrains") 
*/ 
private $authorizedUsers; 

/** 
* Strain constructor. 
*/ 
public function __construct() 
{ 

/** 
* Add authorized user. 
* 
* @param User $user 
* 
* @return $this 
*/ 
public function addAuthorizedUser(User $user) 
{ 
    $this->authorizedUsers[] = $user; 

    return $this; 
} 

/** 
* Remove authorized user. 
* 
* @param User $user 
*/ 
public function removeAuthorizedUser(User $user) 
{ 
    $this->authorizedUsers->removeElement($user); 
} 

/** 
* Get authorized users. 
* 
* @return User|ArrayCollection 
*/ 
public function getAuthorizedUsers() 
{ 
    return $this->authorizedUsers; 
} 

UserRightsType.php

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('authorizedStrains', EntityType::class, array(
      'class' => 'AppBundle\Entity\Strain', 
      'choice_label' => 'name', 
      'expanded' => true, 
      'multiple' => true, 
      'required' => false, 
     )) 
    ; 
} 

public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'AppBundle\Entity\User', 
    )); 
} 

StrainRightsType

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('authorizedUsers', EntityType::class, array(
      'class' => 'AppBundle\Entity\User', 
      'query_builder' => function(UserRepository $ur) { 
       return $ur->createQueryBuilder('u') 
        ->orderBy('u.username', 'ASC'); 
      }, 
      'choice_label' => function ($user) { 
       return $user->getUsername().' ('.$user->getFirstName().' '.$user->getLastName().')'; 
      }, 
      'expanded' => true, 
      'multiple' => true, 
      'required' => false, 
     )) 
    ; 
} 

public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'AppBundle\Entity\Strain', 
    )); 
} 

StrainController.php醜いソリューション

public function userRightsAction(Request $request, Strain $strain) 
{ 
    $form = $this->createForm(StrainRightsType::class, $strain); 
    $form->add('save', SubmitType::class, [ 
     'label' => 'Valid the rights', 
    ]); 

    foreach($strain->getAuthorizedUsers() as $authorizedUser) { 
     $authorizedUser->removeAuthorizedStrain($strain); 
    } 

    $form->handleRequest($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 

     foreach($strain->getAuthorizedUsers() as $authorizedUser) 
     { 
      $authorizedUser->addAuthorizedStrain($strain); 
      $em->persist($authorizedUser); 
     } 

     $em->flush(); 

     $request->getSession()->getFlashBag()->add('success', 'The user\'s rights for the strain '.$strain->getName().' were successfully edited.'); 

     return $this->redirectToRoute('strain_list'); 
    } 

    return $this->render('strain/userRights.html.twig', [ 
     'strain' => $strain, 
     'form' => $form->createView(), 
    ]); 
} 

あなたが見ることができるように、私は2 foreachの操作を行います。すべての権利を削除するために最初にそして第二の権利は権利を与える。

私は

はあなたの助けのために事前にありがとう... symfonyはこの問題を予想しているが、私は方法がわからない、と私はマニュアルには何も見つからなかったと思う、 シェパード

答えて

0

最終的に、私は見つかりました。

で反転側(Strain.php)で

public function addAuthorizedUser(User $user) 
{ 
    $user->addAuthorizedStrain($this); 
    $this->authorizedUsers[] = $user; 

    return $this; 
} 

public function removeAuthorizedUser(User $user) 
{ 
    $user->removeAuthorizedStrain($this); 
    $this->authorizedUsers->removeElement($user); 
} 

そして、所有者側(User.php)

public function addAuthorizedStrain(Strain $strain) 
{ 
    if (!$this->authorizedStrains->contains($strain)) { 
     $this->authorizedStrains[] = $strain; 
    } 

    return $this; 
} 

public function removeAuthorizedStrain(Strain $strain) 
{ 
    if ($this->authorizedStrains->contains($strain)) { 
     $this->authorizedStrains->removeElement($strain); 
    } 
} 

と(逆側の場合)にformTypeに( StrainRightsType))、 'by_reference' => falseを追加する

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('authorizedUsers', EntityType::class, array(
      'class' => 'AppBundle\Entity\User', 
      'query_builder' => function(UserRepository $ur) { 
       return $ur->createQueryBuilder('u') 
        ->orderBy('u.username', 'ASC'); 
      }, 
      'choice_label' => function ($user) { 
       return $user->getUsername().' ('.$user->getFirstName().' '.$user->getLastName().')'; 
      }, 
      'by_reference' => false, 
      'expanded' => true, 
      'multiple' => true, 
      'required' => false, 
     )) 
    ; 
}