title: Doctrine Manytomany with additional field
body: <p>I searchin everywhere something can help me to resolve this issue and did not find.</p>
  
  <p>I have relation:</p>
  
  <p>TABLE A --- one-to-many --- TABLE AB --- many-to-one --- TABLE B</p>
  
  <p>ERROR what i get while try to call newAction method:</p>
  
  <pre><code>Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "AppBundle\Entity\Line#$buttons", got "AppBundle\Entity\Button" instead.
  </code></pre>
  
  <p>I think I have tried everything and get still same error.</p>
  
  <p>Line entity</p>
  
  <pre><code>    namespace AppBundle\Entity;
  
      use Doctrine\Common\Collections\ArrayCollection;
      use Doctrine\ORM\Mapping as ORM;
  
      /**
       * Line
       *
       * @ORM\Table(name="lines")
       * @ORM\Entity(repositoryClass="AppBundle\Repository\LineRepository")
       */
      class Line
      {
          /**
           * @var int
           *
           * @ORM\Column(name="id", type="integer")
           * @ORM\Id
           * @ORM\GeneratedValue(strategy="AUTO")
           */
          private $id;
  
          /**
           * @var string
           *
           * @ORM\Column(name="line_name", type="string", length=255)
           */
          private $lineName;
  
          /**
           * @ORM\OneToMany(targetEntity="AppBundle\Entity\ButtonsState", mappedBy="line", cascade={"all"})
           */
          private $buttons;
  
          public function __construct()
          {
              $this-&gt;buttons = new ArrayCollection();
          }
  
          /**
           * Get id
           *
           * @return int
           */
          public function getId()
          {
              return $this-&gt;id;
          }
  
          /**
           * Set lineName
           *
           * @param string $lineName
           *
           * @return Line
           */
          public function setLineName($lineName)
          {
              $this-&gt;lineName = $lineName;
  
              return $this;
          }
  
          /**
           * Get lineName
           *
           * @return string
           */
          public function getLineName()
          {
              return $this-&gt;lineName;
          }
  
          /**
           * Get buttons
           *
           * @return int
           */
          public function getButtons()
          {
              return $this-&gt;buttons;
          }
  
          public function addButton(Button $button)
          {
              if ($this-&gt;buttons-&gt;contains($button)) {
                  return;
              }
  
              $this-&gt;buttons-&gt;add($button);
          }
  
      }
  </code></pre>
  
  <p>Button entity</p>
  
  <pre><code>namespace AppBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * Button
   *
   * @ORM\Table(name="buttons")
   * @ORM\Entity(repositoryClass="AppBundle\Repository\ButtonRepository")
   */
  class Button
  {
      /**
       * @var int
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\Column(name="button_name", type="string", length=255)
       */
      private $buttonName;
  
      /**
       * @ORM\OneToMany(targetEntity="AppBundle\Entity\ButtonsState", mappedBy="button", cascade={"all"})
       */
      private $lines;
  
  
      /**
       * Get id
       *
       * @return int
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Get buttonName
       *
       * @return string
       */
      public function getButtonName()
      {
          return $this-&gt;buttonName;
      }
  
      /**
       * Set buttonName
       *
       * @param string $buttonName
       *
       * @return Button
       */
      public function setButtonName($buttonName)
      {
          $this-&gt;buttonName = $buttonName;
      }
  
  }
  </code></pre>
  
  <p>ButtonsState entity</p>
  
  <pre><code>namespace AppBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * ButtonsState
   *
   * @ORM\Table(name="lines_buttons")
   * @ORM\Entity(repositoryClass="AppBundle\Repository\ButtonStateRepository")
   */
  class ButtonsState
  {
      /**
       * @ORM\Id()
       * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Line", inversedBy="buttons", cascade={"PERSIST"})
       * @ORM\JoinColumn(name="line_id", referencedColumnName="id")
       */
      private $line;
  
      /**
       * @ORM\Id()
       * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Button", inversedBy="line", cascade={"PERSIST"})
       * @ORM\JoinColumn(name="button_id", referencedColumnName="id")
       */
      private $button;
  
      /**
       * @var int
       *
       * @ORM\Column(name="state", type="integer")
       */
      private $state;
  
      /**
       * @return int
       */
      public function getState()
      {
          return $this-&gt;state;
      }
  
      /**
       * @param int $state
       */
      public function setState($state)
      {
          $this-&gt;state = $state;
      }  
  
  }
  </code></pre>
  
  <p>LineController</p>
  
  <pre><code>/**
       * Creates a new line entity.
       *
       * @Route("/new", name="line_new")
       * @Method({"GET", "POST"})
       */
      public function newAction(Request $request)
      {
          $line = new Line();
          $form = $this-&gt;createForm('AppBundle\Form\LineType', $line);
          $form-&gt;handleRequest($request);
  
          if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) {
  
              $em = $this-&gt;getDoctrine()-&gt;getManager();
  
              foreach ($line-&gt;getButtons() as $button) {
                  $line-&gt;addButton($button);
              }
  
              $em-&gt;persist($line);
              $em-&gt;flush();
  
              return $this-&gt;redirectToRoute('line_index', array('id' =&gt; $line-&gt;getId()));
          }
  
          return $this-&gt;render('line/new.html.twig', array(
              'line' =&gt; $line,
              'form' =&gt; $form-&gt;createView(),
          ));
      }
  </code></pre>
  
  <p>LineType</p>
  
  <pre><code>namespace AppBundle\Form;
  
  use AppBundle\Entity\Button;
  use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  
  class LineType extends AbstractType
  {
      /**
       * {@inheritdoc}
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('lineName', null, array(
                  'label' =&gt; 'Line name',
              ))
              -&gt;add('buttons', EntityType::class, array(
                  'class' =&gt; Button::class,
                  'choice_label' =&gt; 'buttonName',
                  'multiple' =&gt; true,
                  'expanded' =&gt; true,
              ));
      }
  
      /**
       * {@inheritdoc}
       */
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'AppBundle\Entity\Line'
          ));
      }
  
      /**
       * {@inheritdoc}
       */
      public function getBlockPrefix()
      {
          return 'appbundle_line';
      }
  
  
  }
  </code></pre>