Doctrine flush() error: Expected value of type "Doctrine\Common\Collections\Collection|array"
<p>I have a strange problem using a many-to-many relation in Symfony (with Doctrine), I've never had before in symfony projects with many-to-many relations and I can't find any difference to the other projects. </p>

<p>I have the two entitys Product and Tag and a many-to-many relation to each other. Unfortunately, if I try to add a product to a tag or vice versa, the error </p>

<blockquote>
  <p>Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "TestBundle\Entity\Product#$tags", got "TestBundle\Entity\Tag" instead.</p>
</blockquote>

<p>appears. 
The code used to add a tag to a product:</p>

<pre><code>    $tag1 = $em-&gt;getRepository('TestBundle:Tag')-&gt;findOneBy(array(
        'tag' =&gt; "Bla"
    ));

    $tag1-&gt;addProduct($product);
    $em-&gt;persist($tag1);
    $em-&gt;persist($product);

    $em-&gt;flush();
</code></pre>

<p>Of course, the variable $tag1 and $product both contain a valid entity. 
The YAML file for the many-to-many relations (I cut away irrelevant parts):</p>

<pre><code>TestBundle\Entity\Tag:
type: entity
table: tags
repositoryClass: TestBundle\Repository\TagRepository
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    tag:
        type: string
        length: 255
        unique: true
manyToMany:
    products:
        targetEntity: Product
        mappedBy: tags
lifecycleCallbacks: {  }
</code></pre>

<p>Product:</p>

<pre><code>TestBundle\Entity\Product:
type: entity
table: products
repositoryClass: TestBundle\Repository\ProductRepository
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    name:
        type: string
        length: 255
        unique: true
manyToOne:
    manufacturer:
        targetEntity: Manufacturer
        inversedBy: products
        joinColumn:
            name: manufacturer_id
            referencedColumnName: id
            onDelete: CASCADE
manyToMany:
    tags:
        targetEntity: Product
        inversedBy: products
        joinTable:
            name: tags2products
            joinColumns:
                tag_id:
                    referencedColumnName: id
            inverseJoinColumns:
                product_id:
                    referencedColumnName: id
lifecycleCallbacks: {  }
</code></pre>

<p>The setter and getter functions also don't contain any special tricks:
The Tag.php entity file contains:</p>

<pre><code> /**
 * Constructor
 */
public function __construct()
{
    $this-&gt;product = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Add product
 *
 * @param \TestBundle\Entity\Product $product
 *
 * @return Tag
 */
public function addProduct(\TestBundle\Entity\Product $product)
{
    $product-&gt;addTag($this);
    $this-&gt;product[] = $product;
    return $this;
}


public function removeProduct(\TestBundle\Entity\Product $product)
{
    $this-&gt;product-&gt;removeElement($product);
}

/**
 * Get products
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getProducts()
{
    return $this-&gt;products;
}
</code></pre>

<p>While the Product.php contains:</p>

<pre><code> /**
 * Add tag
 *
 * @param \TestBundle\Entity\Tag $tag
 *
 * @return Product
 */
public function addTag(Tag $tag)
{
    $this-&gt;tags-&gt;add($tag);
    //$this-&gt;tags[] = $tag;
    return $this;
}

/**
 * Remove tag
 *
 * @param \TestBundle\Entity\Tag $webpage
 */

public function removeTag(Tag $tag)
{
    $this-&gt;tags-&gt;removeElement($tag) ;
}

/**
 * Get webpages
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getTags()
{
    return $this-&gt;tags;
}
</code></pre>

<p>I also tried to add a $this->tags = new ArrayCollection(); to the constructor of the product, but it didnt change anything. </p>

<p>Also, there is no problem adding, reading and persisting tags to products. The error is thrown as soon as I call $em->flush().</p>

<p>Does anybody know why my Product entity expects a array collection? I never told him to expect one! Thank you very much in advance!</p>