title: Type error with ArrayCollection / OneToMany relationship in Symfony 3.4
body: <p>For the past couple of days I have been trying to create a bidirectionnal ManyToOne-OneToMany relationship in Symfony 3.4
  I have two entities. One is Contribution and the other is Source. A Contribution can have several sources. So the relationship should be </p>
  
  <blockquote>
    <p>Contribution – ManyToOne – Source – OneToMany – Contribution</p>
  </blockquote>
  
  <p>But I keep getting the following error during <code>$em→flush();</code> in my controller:</p>
  
  <blockquote>
    <p>Type error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in /var/www/html/Edebate/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 605</p>
  </blockquote>
  
  <p>I do not have any set method related to the Array Collection in my Entity Contribution as I could see in other posts here:</p>
  
  <p><a href="https://stackoverflow.com/questions/39638572/type-error-argument-1-passed-to-doctrine-common-collections-arraycollection">Type error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given</a></p>
  
  <p><a href="https://stackoverflow.com/questions/47883046/symfony-catchable-fatal-error-argument-1-passed-to-doctrine-common-collections">Symfony-Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given</a></p>
  
  <p>And the annotations are ok as mentionned here:</p>
  
  <p><a href="https://stackoverflow.com/questions/17583237/doctrine-onetomany-relationship-error">Doctrine OneToMany relationship error</a></p>
  
  <p>Any help would be appreciate ! :)</p>
  
  <p>Here is my Entity Contribution</p>
  
  <pre><code>use Doctrine\Common\Collections\ArrayCollection;
  
  //annotations
  
    abstract class Contribution
      {
  
      /**
      * @ORM\OneToMany(targetEntity="Shaker\DebateBundle\Entity\Source", mappedBy="parent")
      */
      protected $sources;
  
  //Other attributes and methods
  
  
      public function __construct() {
         $this-&gt;sources = new ArrayCollection();
      }
  
  
      /**
       * Add source
       *
       * @param \Shaker\DebateBundle\Entity\Source $source
       *
       * @return Contribution
       */
      public function addSource(\Shaker\DebateBundle\Entity\Source $source)
      {
          $this-&gt;sources[] = $source;
          return $this;
      }
  
      /**
       * Remove source
       *
       * @param \Shaker\DebateBundle\Entity\Source $source
       */
      public function removeSource(\Shaker\DebateBundle\Entity\Source $source)
      {
          $this-&gt;sources-&gt;removeElement($source);
      }
  
      /**
       * Get sources
       *
       * @return \Doctrine\Common\Collections\Collection
       */
      public function getSources()
      {
          return $this-&gt;sources;
      }
  }
  </code></pre>
  
  <p>And this is in my Entity Source:</p>
  
  <pre><code>/**
  * @ORM\ManyToOne(targetEntity="Shaker\DebateBundle\Entity\Contribution", inversedBy="sources")
  */
  protected $parent;
  
     /**
       * Set parent
       *
       * @param \Shaker\DebateBundle\Entity\Contribution $parent
       *
       * @return Contribution
       */
      public function setParent(\Shaker\DebateBundle\Entity\Contribution $parent = null)
      {
          $this-&gt;parent = $parent;
          $parent-&gt;addSource($this);
          return $this;
      }
  
      /**
       * Get parent
       *
       * @return \Shaker\JRQBundle\Entity\Contribution
       */
      public function getParent()
      {
          return $this-&gt;parent;
      }
  </code></pre>
  
  <p>And in my Controller, the problem arises with flush:</p>
  
  <pre><code>        $formsourcebook-&gt;handleRequest($request);
          $contributionid=$formsourcebook-&gt;get('ContributionId')-&gt;getData();
  
          if ($formsourcebook-&gt;isValid()) {
              $topicargtarget=$this-&gt;getContribution($contributionid);
              $sourcebook-&gt;setUser($user);
              $sourcebook-&gt;setContribution($topicargtarget);
              $em-&gt;persist($sourcebook);
              $em-&gt;flush();
          }
  </code></pre>