title: How to persist one object with many related objects
body: <p>I have entities <code>Basket</code> and <code>BasketItem</code>:</p>
  
  <pre><code>/**
   * Acme\BasketBundle\Entity\Basket
   *
   * @ORM\Entity(repositoryClass="Acme\BasketBundle\Repository\BasketRepository")
   * @ORM\Table(name="orders")
   * @ORM\HasLifecycleCallbacks()
   */
  class Basket
  {
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
  
    // ...
  
    /**
      * @ORM\OneToMany(targetEntity="BasketItem", mappedBy="order_id",cascade={"all"})
      */
    protected $items;
  
    // ...
  
    public function __construct() {
      $this-&gt;items = new ArrayCollection();
    }
  
    /**
     * Add item
     *
     * @param BasketItem $item
     */
    public function addItem(BasketItem $item)
    {
      $key = $this-&gt;find($item-&gt;getProduct()-&gt;getId());
  
      if ($key === false) {
          $this-&gt;items-&gt;add($item);
      } else {
          $this-&gt;items-&gt;get($key)-&gt;raiseQuantity($item-&gt;getQuantity());
      }
    }
  
    /**
     * Find an item (if present)
     *
     * @param integer $id
     * @return integer
     */
    public function find($id)
    {
      foreach ($this-&gt;items as $key =&gt; $item) {
          if ($item-&gt;getProduct()-&gt;getId() == $id)
              return $key;
      }
      return false;
    }
  }
  </code></pre>
  
  <p><br /></p>
  
  <pre><code>/**
   * Acme\BasketBundle\Entity\BasketItem
   *
   * @ORM\Entity
   * @ORM\Table(name="order_items")
   */
  class BasketItem
  {
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
  
    // ...
  
    /**
     * @ORM\ManyToOne(targetEntity="Basket", inversedBy="items")
     * @ORM\JoinColumn(name="order_id", referencedColumnName="id")
     */
    private $basket;
  
    // ...
  }
  </code></pre>
  
  <hr>
  
  <p>Now when I create a basket and fill it with items, I have trouble persisting it into the db.</p>
  
  <p>Following doesn't work as expected.</p>
  
  <pre><code>$basket = new Basket();
  $basket-&gt;addItem(new BasketItem($product1, 1));
  $basket-&gt;addItem(new BasketItem($product2, 2));
  
  $em-&gt;persist($basket);
  $em-&gt;flush();
  </code></pre>
  
  <p>Then I tried following the <a href="http://symfony.com/doc/2.0/book/doctrine.html#saving-related-entities" rel="noreferrer">manual</a> more closely:</p>
  
  <pre><code>$basket = new Basket();
  $basket-&gt;addItem(new BasketItem($product1, 1));
  $basket-&gt;addItem(new BasketItem($product2, 2));
  
  $em-&gt;persist($basket);
  foreach ($basket-&gt;getItems() as $item) {
      $em-&gt;persist($item);
  }
  $em-&gt;flush();
  </code></pre>
  
  <p>That didn't work as expected neither.</p>
  
  <p>In both cases all the data gets saved to the database <strong>but</strong> the basket items are not related to the basket, i.e. <code>order_id</code> of the <code>BasketItem</code> entity is <code>NULL</code>.</p>
  
  <p>Can anyone explain me what I'm doing wrong, please? Please note I'm new to Doctrine. Thanks!</p>
  
  <hr>
  
  <hr>
  
  <hr>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>I'm really confused already. Here's my simplified test:</p>
  
  <pre><code>&lt;?php
  
  namespace Amsel\BasketBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * Amsel\BasketBundle\Entity\Basket
   *
   * @ORM\Entity(repositoryClass="Amsel\BasketBundle\Repository\BasketRepository")
   * @ORM\Table(name="orders")
   */
  class Basket
  {
      /**
       * @var integer $id
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var ArrayCollection $items
       *
       * @ORM\OneToMany(targetEntity="BasketItem", mappedBy="basket", cascade={"all"})
       */
      protected $items;
  
      public function __construct() {
          $this-&gt;items = new ArrayCollection();
      }
  
      /**
       * Add item
       *
       * @param BasketItem $item
       */
      public function addItem(BasketItem $item)
      {
          $this-&gt;items-&gt;add($item);
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Get items
       *
       * @return Doctrine\Common\Collections\Collection 
       */
      public function getItems()
      {
          return $this-&gt;items;
      }
  }
  </code></pre>
  
  <p><br /></p>
  
  <pre><code>&lt;?php
  
  namespace Amsel\BasketBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  
  /**
   * Amsel\BasketBundle\Entity\BasketItem
   *
   * @ORM\Entity(repositoryClass="Amsel\BasketBundle\Repository\BasketItemRepository")
   * @ORM\Table(name="order_items")
   */
  class BasketItem
  {
      /**
       * @var integer $id
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @var Basket $basket
       *
       * @ORM\ManyToOne(targetEntity="Basket", inversedBy="items")
       * @ORM\JoinColumn(name="order_id", referencedColumnName="id")
       */
      protected $basket;
  
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set basket
       *
       * @param Amsel\BasketBundle\Entity\Basket $basket
       */
      public function setBasket(\Amsel\BasketBundle\Entity\Basket $basket)
      {
          $this-&gt;basket = $basket;
      }
  
      /**
       * Get basket
       *
       * @return Amsel\BasketBundle\Entity\Basket 
       */
      public function getBasket()
      {
          return $this-&gt;basket;
      }
  }
  </code></pre>
  
  <p><br /></p>
  
  <pre><code>public function testAction(Request $request) {
  
      $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
  
      $basket = new Basket();
      $basket-&gt;addItem(new BasketItem());
      $basket-&gt;addItem(new BasketItem());
  
      $em-&gt;persist($basket);
  
      try {
          $em-&gt;flush();
      } catch(Exception $e) {
          die('ERROR: '.$e-&gt;getMessage());
      }        
      die ('end');
  }
  </code></pre>
  
  <p>But still - the order items (<code>BasketItem</code>) get stored but are <em>not</em> linked to the order (<code>Basket</code>).</p>