title: Symfony Form to store a selection of User Interests to UserInterests
body: <p>My aim is to use a Symfony Form to allow a user to choose multiple interests and save these relationally against their user.</p>
  
  <p>I have three entities to map and store this data:</p>
  
  <pre><code>User
   - id
   - name
  
  Interest
   - id
   - name
  
  UserInterest
   - id
   - user_id (FK ManyToOne user.id)
   - interest_id (FK ManyToOne interest.id)
  </code></pre>
  
  <p>I'm struggling to find the most dynamic <em>Symfony</em> way to process and save the <code>Interest</code>s of a <code>User</code> to the <code>UserInterest</code> entity.</p>
  
  <p><strong><em>InterestsController.php</em></strong></p>
  
  <pre><code>public function interests(Request $request)
  {
      $error = null;
  
      $userInterests = new UserInterest();
      $userInterests-&gt;setUser($this-&gt;getUser());
  
      $form = $this-&gt;createForm(UserAccountInterests::class, $userInterests);
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
          try {
              $this-&gt;entityManager-&gt;persist($userInterests);
              $this-&gt;entityManager-&gt;flush();
      } catch (\Exception $e) {
              $error = $e-&gt;getMessage();
          }
      }
  
      $parameters = [
          'error' =&gt; $error,
          'user_interests_form' =&gt; $form-&gt;createView()
      ];
  
      return $this-&gt;render('user/interests.html.twig', $parameters);
  }
  </code></pre>
  
  <p><strong><em>UserInterestsType.php</em></strong></p>
  
  <pre><code>class UserInterestsType extends AbstractType
  {
      /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
      $builder
          -&gt;add('interest', EntityType::class, [
              'class' =&gt; Interest::class,
              'choice_label' =&gt; 'name',
              'expanded' =&gt; true,
              'multiple' =&gt; true
          ])
          -&gt;add('update', SubmitType::class);
      }
   }
  </code></pre>
  
  <h1>The problem</h1>
  
  <p>The issue I'm facing at the moment is that upon form submission this creates an instantiation of the <code>UserInterest</code> entity linked to a <code>user</code> but with multiple selected <code>Interest</code> as an array and not multiple <code>UserInterest</code>.</p>
  
  <p>How can I do this in the proper Symfony way to allow for one form and simple controller logic so the user can load the page, select their interests from a multiple checkbox type, click save and then when the form is reloaded they are auto populated as prior selection?</p>