title: Symfony (Doctrine) 2: Entity ManyToMany Relationship with itself
body: <p>I have an entity "Product". A Product can be a "package" (boolean) which then consists of Products. A Product can be a part of many packages. As such, I need to map a ManyToMany relationship in Product with itself, so that I have $packageProducts in a mapping table with "package_id" and "product_id".</p>
  
  <p>How do I define the ManyToMany relationship inside the "Product" entity to map to itself? I know you need to define your join table and you mappedColumn and inverseColumn, but I do not know exactly how to do this.</p>
  
  <p>Current Product Entity (ManyToMany mapping not yet included):</p>
  
  <pre><code>&lt;?php
  
  namespace LittleGiant\PortalBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\Criteria;
  use Doctrine\Common\Collections\ArrayCollection;
  use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  use Symfony\Component\Validator\Constraints as Assert;
  use JMS\Serializer\Annotation as Ser;
  use Gedmo\Mapping\Annotation as Gedmo;
  
  /**
   * Product
   *
   * @ORM\Entity
   * @ORM\Table(name="products")
   * @Ser\ExclusionPolicy("all")
   */
  class Product
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       * @Ser\Expose
       */
      protected $id;
  
      /**
       * @var boolean
       *
       * @ORM\Column(name="package", type="boolean")
       * @Ser\Expose
       */
      protected $package;
  
      /**
       * @var boolean
       *
       * @ORM\Column(name="active", type="boolean")
       * @Ser\Expose
       */
      protected $active;
  
      /**
       * @var string
       *
       * @ORM\Column(name="product_range", type="string", length=32)
       * @Ser\Expose
       */
      protected $range;
  
      /**
       * @var string
       *
       * @ORM\Column(name="name", type="string", length=128)
       * @Ser\Expose
       */
      protected $name;
  
      /**
       * @var text
       *
       * @ORM\Column(name="description", type="text")
       * @Ser\Expose
       */
      protected $description;
  
      /**
       * @var File
       * 
       * @ORM\OneToOne(targetEntity="LittleGiant\PortalBundle\Entity\File", cascade={"remove","persist"})
       * @Ser\Expose
       **/
      protected $displayImage;
  
      /**
       * @var float
       *
       * @ORM\Column(name="price", type="float")
       * @Ser\Expose
       */
      protected $price;
  
      /**
       * @var Deliverable[]
       * 
       * @ORM\OneToMany(targetEntity="LittleGiant\PortalBundle\Entity\Deliverable", mappedBy="product", cascade={"remove"})
       **/
      protected $deliverables;
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> When I add the following to my Product Entity</p>
  
  <pre><code>/**
   * @var Product[]
   * 
   * @ORM\ManyToMany(targetEntity="LittleGiant\PortalBundle\Entity\Product", mappedBy="packageProducts")
   **/
  protected $packages;
  
  /**
   * @var Product[]
   * 
   * @ORM\ManyToMany(targetEntity="LittleGiant\PortalBundle\Entity\Product", inversedBy="packages")
   **/
  protected $packageProducts;
  </code></pre>
  
  <p>I get a mapping table "product_product" with a single field "product_id". I need this to be "package_products" with fields "product_id", "package_id". How do I define this in the ORM?</p>