title: Symfony 3 Many To Many not saving data to ManyToMany Table
body: <p>I started project with predefined database structure and generated Entities from database structure using commands from console. I am creating bus company. Bus can have many amenities (i.e. Many buses can have many amenities).</p>
  
  <p>I have a ManyToMany table and for some reason data inside that table is not saving. Other form data is saved in other table but this one still remains empty.</p>
  
  <p>Does someone knows where is the problem?</p>
  
  <p>Here is the code:</p>
  
  <p>BusVehicles.php</p>
  
  <pre><code>     /**
           * Many Buses have Many Amenities.
           * @ORM\ManyToMany(targetEntity="BusVehicles", mappedBy="bus_amenities")
           * @ORM\JoinTable(
           *  name="bus_amenities",
           *  joinColumns={
           *      @ORM\JoinColumn(name="bus_id", referencedColumnName="id")
           *  },
           *  inverseJoinColumns={
           *      @ORM\JoinColumn(name="amenities_id", referencedColumnName="id")
           *  }
           * )
           */
          private $amenities;
      /**
       * Add items
       *
       * @param \AdminBundle\Entity\Amenities $amenities
       */
  
      public function addAmenities(Amenities $amenities)
      {
          if ($this-&gt;amenities-&gt;contains($amenities)) {
              return;
          }
          $this-&gt;amenities-&gt;add($amenities);
          $amenities-&gt;addBusAmenities($this);
      }
  
      /**
       * Remove amenities
       *
       * @param \AdminBundle\Entity\Amenities $amenities
       */
      public function removeAmenities(Amenities $amenities)
      {
          if (!$this-&gt;amenities-&gt;contains($amenities)) {
              return;
          }
          $this-&gt;amenities-&gt;removeElement($amenities);
          $amenities-&gt;removeBusAmenities($this);
      }
  
      /**
       * Get Amenities
       *
       * @return ArrayCollection
       */
  
      public function getAmenities()
      {
          return $this-&gt;amenities;
      }
  
  
      // ...
  
      public function __construct() {
          $this-&gt;amenities = new ArrayCollection();
      }
  } 
  </code></pre>
  
  <p>Amenities.php</p>
  
  <pre><code>    /**
       * @var ArrayCollection
       *
       * @ORM\ManyToMany(targetEntity="BusVehicles", mappedBy="amenities")
       */
      private $bus_amenities;
  
      public function __construct() {
          $this-&gt;bus_amenities = new ArrayCollection();
      }
      /**
       * Get bus amenities
       *
       * @return ArrayCollection
       */
  
      public function getBusAmenities()
      {
          return $this-&gt;bus_amenities;
      }
  
  
  
      /**
       * Add bus amenities
       *
       * @param \AdminBundle\Entity\BusVehicles
       */
  
      public function addBusAmenities(BusVehicles $amenities)
      {
  
          if ($this-&gt;bus_amenities-&gt;contains($amenities)) {
              return;
          }
          $this-&gt;bus_amenities-&gt;add($amenities);
          $amenities-&gt;addAmenities($this);
  
  
      }
  
      /**
       * Remove bus amenities
       *
       * @param \AdminBundle\Entity\BusVehicles
       */
      public function removeBusAmenities(BusVehicles $amenities)
      {
  
          if (!$this-&gt;bus_amenities-&gt;contains($amenities)) {
              return;
          }
          $this-&gt;bus_amenities-&gt;removeElement($amenities);
          $amenities-&gt;removeAmenities($this);
      }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>class BusController extends Controller
  {
      /**
       * @Template
       */
      public function addAction(Request $request)
      {
          $bus = new BusVehicles();
          $form = $this-&gt;createForm(BusVehiclesType::class, $bus);
          $form-&gt;handleRequest($request);
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()-&gt;getManager();
             /* $amenities = $em-&gt;getAmenities();
              foreach($amenities as $amenitie){
                  $em-&gt;persist($amenitie);
              }*/
              $em-&gt;persist($bus);
  
              $em-&gt;flush();
  
              // Adding flash message to our user
  
              $request-&gt;getSession()
                  -&gt;getFlashBag()
                  -&gt;add('success', 'New vehicle successfully added');
  
  
            //  return $this-&gt;redirectToRoute('bus_add');
  
          }
          return [
              'form' =&gt; $form-&gt;createView(),
          ];
      }
  
  
  }
  </code></pre>
  
  <p><strong>EDIT 1: Added form</strong></p>
  
  <pre><code>class BusVehiclesType extends AbstractType
  {
      /**
       * {@inheritdoc}
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('licencePlate')
              -&gt;add('manufacturer')         
              -&gt;add('company', EntityType::class, array(
                  'class' =&gt; 'AdminBundle:Companies',
                  'choice_label' =&gt; 'name',
              ))
              -&gt;add('busType', EntityType::class, array(
                  'class' =&gt; 'AdminBundle:BusTypes',
                  'choice_label' =&gt; 'type',
              ))
              -&gt;add('emissionClass', EntityType::class, array(
                  'class' =&gt; 'AdminBundle:BusEmissionClasses',
                  'choice_label' =&gt; 'name',
              ))
              -&gt;add('fuelType', EntityType::class, array(
                  'class' =&gt; 'AdminBundle:BusFuelTypes',
                  'choice_label' =&gt; 'fuel_type',
              ))
              -&gt;add('amenities', EntityType::class, array(
                  'class' =&gt; 'AdminBundle:Amenities',
                  'choice_label' =&gt; 'name',
                  'multiple' =&gt; true,
              ));
      }
  
      /**
       * {@inheritdoc}
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'AdminBundle\Entity\BusVehicles'
          ));
      }
  
      /**
       * {@inheritdoc}
       */
      public function getBlockPrefix()
      {
          return 'adminbundle_busvehicles';
      }
  
  
  }
  </code></pre>