title: Doctrine 2 Many to Many relation with additional columns in join table
body: <p>So, I have been playing round with using doctrine for a while now and have it in some basic projects, but i decided to go back and have an in depth look into what it can do.</p>
  
  <p>Ive now decided to switch to symfony 2 as my framework of choice and am looking into what doctrine 2 can do in more depth.</p>
  
  <p>One thing i have been trying to get my head around is the many to many relationship within doctrine. I am starting to build a recipe system and am working on the relation between recipe and ingredients which gave me 3 entities, recipe, recipeIngredient and ingredient. The reason i cannot use a direct many to many relation is because i want to store two additional columns in the join table ( unit and quantity ) for each ingredient.</p>
  
  <p>The problem i am having at the moment is that the entities persist ok, but the recipe_id in the join table is not inserted. I have tried everything i can think off and been through every thread and website looking for an answer . I am sure it is something completely obvious that i am missing. Please help, below is the code i have so far:</p>
  
  <pre><code>&lt;?php
  namespace Recipe\RecipeBundle\Entity;
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  /**
   * @ORM\Entity
   * @ORM\Table(name="recipe")
   * @ORM\HasLifecycleCallbacks()
   */
  class Recipe{
  
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;
  
  /**
   * @ORM\OneToMany(targetEntity="RecipeIngredient", mappedBy="recipe", cascade=       {"persist"})
   */
  protected $ingredients;
  /**
   * @ORM\Column(type="string")
   * @var string $title
   *
   */
  protected $title;
  /**
   * Constructor
   */
  public function __construct()
  {
      $this-&gt;ingredients = new \Doctrine\Common\Collections\ArrayCollection();
  }
  
  /**
   * Get id
   *
   * @return integer
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Add ingredients
   *
   * @param \Recipe\RecipeBundle\Entity\RecipeIngredient $ingredients
   * @return Recipe
   */
  public function addIngredient(\Recipe\RecipeBundle\Entity\RecipeIngredient $ingredients)
  {
      $ingredients-&gt;setRecipe($this);
      $this-&gt;ingredients[] = $ingredients;
  
      return $this;
  }
  
  /**
   * Remove ingredients
   *
   * @param \Recipe\RecipeBundle\Entity\RecipeIngredient $ingredients
   */
  public function removeIngredient(\Recipe\RecipeBundle\Entity\RecipeIngredient $ingredients)
  {
      $this-&gt;ingredients-&gt;removeElement($ingredients);
  }
  
  /**
   * Get ingredients
   *
   * @return \Doctrine\Common\Collections\Collection
   */
  public function getIngredients()
  {
      return $this-&gt;ingredients;
  }
  
  /**
   * Set title
   *
   * @param string $title
   * @return Recipe
   */
  public function setTitle($title)
  {
      $this-&gt;title = $title;
  
      return $this;
  }
  
  /**
   * Get title
   *
   * @return string
   */
  public function getTitle()
  {
      return $this-&gt;title;
  }
  }
  </code></pre>
  
  <p>and recipeIngredient
      
  
  <pre><code>/**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;
  
  /**
   * @ORM\ManyToOne(targetEntity="Recipe", inversedBy="ingredients")
   * */
  protected $recipe;
  
  /**
   * @ORM\ManyToOne(targetEntity="Ingredient", inversedBy="ingredients" , cascade={"persist"})
   * */
  protected $ingredient;
  
  /**
   * @ORM\Column(type="string")
   * @var string  $quantity
   *
   */
  protected $quantity;
  
  /**
   * @ORM\Column(type="string")
   * @var string $unit
   *
   */
  protected $unit;
  
  /**
   * Get id
   *
   * @return integer
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set quantity
   *
   * @param string $quantity
   * @return RecipeIngredient
   */
  public function setQuantity($quantity)
  {
      $this-&gt;quantity = $quantity;
  
      return $this;
  }
  
  /**
   * Get quantity
   *
   * @return string
   */
  public function getQuantity()
  {
      return $this-&gt;quantity;
  }
  
  /**
   * Set unit
   *
   * @param string $unit
   * @return RecipeIngredient
   */
  public function setUnit($unit)
  {
      $this-&gt;unit = $unit;
  
      return $this;
  }
  
  /**
   * Get unit
   *
   * @return string
   */
  public function getUnit()
  {
      return $this-&gt;unit;
  }
  
  /**
   * Set recipe
   *
   * @param \Recipe\RecipeBundle\Entity\Recipe $recipe
   * @return RecipeIngredient
   */
  public function setRecipe(\Recipe\RecipeBundle\Entity\Recipe $recipe = null)
  {
      $this-&gt;recipe = $recipe;
  
      return $this;
  }
  
  /**
   * Get recipe
   *
   * @return \Recipe\RecipeBundle\Entity\Recipe
   */
  public function getRecipe()
  {
      return $this-&gt;recipe;
  }
  
  /**
   * Set ingredient
   *
   * @param \Recipe\RecipeBundle\Entity\Ingredient $ingredient
   * @return RecipeIngredient
   */
  public function setIngredient(\Recipe\RecipeBundle\Entity\Ingredient $ingredient = null)
  {
      $this-&gt;ingredient = $ingredient;
  
      return $this;
  }
  
  /**
   * Get ingredient
   *
   * @return \Recipe\RecipeBundle\Entity\Ingredient
   */
  public function getIngredient()
  {
      return $this-&gt;ingredient;
  }
  }
  </code></pre>