title: Symfony 3.4 - Doctrine ManyToMany association error
body: <p>I'm working on a simple Symfony API and I'm having trouble setting a relation between two entities.
  I have this two classes: Recipe and Ingredient.
  A Recipe can have many ingredients and an ingredient can be present in many recipes so I used a ManyToMany association.</p>
  
  <p>This is my Recipe class:</p>
  
  <pre><code>namespace RecipesBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * Recipe
   *
   * @ORM\Table(name="recipe")
   * @ORM\Entity(repositoryClass="RecipesBundle\Repository\RecipeRepository")
   */
  class Recipe
  {
  /**
   * @var int
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @var string
   *
   * @ORM\Column(name="name", type="string", length=255)
   */
  private $name;
  
  /**
   * @var string
   *
   * @ORM\Column(name="description", type="string", length=255, nullable=true)
   */
  private $description;
  
  /**
   * @var string
   *
   * @ORM\Column(name="image", type="string", length=255, nullable=true)
   */
  private $image;
  
  /**
   * @ORM\ManyToMany(targetEntity="Ingredient", inversedBy="recipes")
   * @ORM\JoinTable(name="recipes_ingredients")
   */
  private $ingredients;
  
  public function __construct() {
      $this-&gt;ingredients = new ArrayCollection();
  }
  
  /**
   * Get id
   *
   * @return int
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set name
   *
   * @param string $name
   *
   * @return Recipe
   */
  public function setName($name)
  {
      $this-&gt;name = $name;
  
      return $this;
  }
  
  /**
   * Get name
   *
   * @return string
   */
  public function getName()
  {
      return $this-&gt;name;
  }
  
  /**
   * Set description
   *
   * @param string $description
   *
   * @return Recipe
   */
  public function setDescription($description)
  {
      $this-&gt;description = $description;
  
      return $this;
  }
  
  /**
   * Get description
   *
   * @return string
   */
  public function getDescription()
  {
      return $this-&gt;description;
  }
  
  /**
   * Set image
   *
   * @param string $image
   *
   * @return Recipe
   */
  public function setImage($image)
  {
      $this-&gt;image = $image;
  
      return $this;
  }
  
  /**
   * Get image
   *
   * @return string
   */
  public function getImage()
  {
      return $this-&gt;image;
  }
  
  /**
   * Set ingredients
   * 
   * @param array $ingredients
   * @return Recipe
   */
  public function setIngredients($ingredients) {
  
      foreach($ingredients as $ingredient) {
          $this-&gt;ingredients-&gt;add($ingredient);
      }
  
      return $this;
  }
  
  /**
   * Get ingredients
   * 
   * @return ArrayCollection
   */
  public function getIngredients() {
      return $this-&gt;ingredients;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is my Ingredient class:</p>
  
  <pre><code>namespace RecipesBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * Ingredient
   *
   * @ORM\Table(name="ingredient")
   * 
  @ORM\Entity(repositoryClass="RecipesBundle\Repository\IngredientRepository")
   */
  class Ingredient
  {
  /**
   * @var int
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @var string
   *
   * @ORM\Column(name="name", type="string", length=255)
   */
  private $name;
  
  /**
   * @var int
   *
   * @ORM\Column(name="amount", type="integer")
   */
  private $amount;
  
  /**
   * @ORM\ManyToMany(targetEntity="Recipe", mappedBy="ingredients")
   */
  private $recipes;
  
  public __construct() {
      $this-&gt;recipes = new ArrayCollection();
  }
  
  /**
   * Get id
   *
   * @return int
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set name
   *
   * @param string $name
   *
   * @return Ingredient
   */
  public function setName($name)
  {
      $this-&gt;name = $name;
  
      return $this;
  }
  
  /**
   * Get name
   *
   * @return string
   */
  public function getName()
  {
      return $this-&gt;name;
  }
  
  /**
   * Set amount
   *
   * @param integer $amount
   *
   * @return Ingredient
   */
  public function setAmount($amount)
  {
      $this-&gt;amount = $amount;
  
      return $this;
  }
  
  /**
   * Get amount
   *
   * @return int
   */
  public function getAmount()
  {
      return $this-&gt;amount;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And this is my controller:</p>
  
  <pre><code>class RecipeController extends Controller
  {
  /**
   * @Route("/recipes")
   */
  public function recipesAction(Request $request)
  {
      switch($request-&gt;getMethod()) {
          case 'GET':
  
              $recipes = $this-&gt;getDoctrine()-&gt;getRepository(Recipe::class)-&gt;findAll();
              $return = $recipes;
  
              break;
          case 'POST':
              $recipe = new Recipe();
              $recipeData = json_decode($request-&gt;getContent(), true);
  
              $recipe-&gt;setName($recipeData['name']);
              $recipe-&gt;setDescription($recipeData['description']);
              $recipe-&gt;setImage($recipeData['imagePath']);
  
              $ingredients = $recipeData['ingredients'];
  
              //var_dump($ingredients);die;
  
              $recipe-&gt;setIngredients($ingredients);
  
              //var_dump($recipe);die;
  
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($recipe);
              $em-&gt;flush();
  
              $return = 'Recipe saved successfully';
  
              break;
          case 'PUT':
  
              $recipes = json_decode($request-&gt;getContent(), true);
  
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              foreach($recipes as $updating_recipe) {
  
                  $recipe = $this-&gt;getDoctrine()-&gt;getRepository(Recipe::class)-&gt;findOneBy(array('name' =&gt; $updating_recipe['name']));
  
                  if($recipe)
                  {
                      $recipe-&gt;setName($updating_recipe['name']);
                      $recipe-&gt;setDescription($updating_recipe['description']);
                      $recipe-&gt;setImage($updating_recipe['image']);
                      $recipe-&gt;setIngredients($updating_recipe['ingredients']);
  
                      $em-&gt;flush();
                  }
              }
  
              $return = 'Recipes saved successfully';
              break;
          case 'DELETE':
              break;
      }
      return new JsonResponse($return);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The problem occurs when I try to save a new recipe (post request).
  I get the following error:</p>
  
  <p>Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "RecipesBundle\Entity\Recipe#$ingredients", got "array" instead.</p>
  
  <p>I don't understand why I get this error if I use the setIngredients correctly to add the ingredients to the array collection.</p>
  
  <p>Any ideas on what could be wrong?
  Thanks in advance</p>
  
  <p>EDIT 1:</p>
  
  <p>I'm adding the dump of the ingredients as requested:</p>
  
  <pre><code>array (size=1) 
      0 =&gt; 
          array (size=2) 
              'name' =&gt; string 'Spaghetti' (length=9)
              'amount' =&gt; int 10
  </code></pre>