title: Doctrine2 Map entities with composite foreign keys in the composite primary keys
body: <p>I have a model which has many tables, but in this case we only need three.</p>
  
  <p>The point is that the composite primary key of one is also the foreing key (composite too) and Symfony throws this exception:</p>
  
  <p><strong>MappingException: It is not possible to map entity <em>'Your\SomethingBundle\Entity\Empleado'</em> with a composite primary key as part of the primary key of another entity <em>'Your\SomethingBundle\Entity\EmpleadoHorario#empleado'</em>.</strong></p>
  
  <p>Here I explain the relationship:</p>
  
  <p>1º Salon, it has a primary key <strong>ID</strong></p>
  
  <p>2º Empleado, it has a composite primary key <strong>ID, Salon_id</strong> and, also in the primary key, a foreing key referencing Salon: <strong>Salon_id</strong></p>
  
  <p>3º EmpleadoHorario: it has a composite primary key <strong>Fecha, Empleado_id, Salon_id</strong> and, also in the primary key, two a foreing keys referencing Salon: <strong>Salon_id</strong>, and Empleado: <strong>Empleado_id, Salon_id</strong></p>
  
  <p>All the relations has also the inverse union. Here is the code:</p>
  
  <p>The <strong>Salon</strong> Entity:</p>
  
  <pre><code>/**
   * Salon
   *
   * @ORM\Table(name="salon")
   * @ORM\Entity
   */
  class Salon
  {
      /**
       * @var string
       *
       * @ORM\Column(name="id", type="string", length=50, nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="IDENTITY")
       */
      private $id;
  
      // More fields...
  
      /**
       * @var array_collection 
       * 
       * @ORM\OneToMany(targetEntity="Empleado", mappedBy="salon")
       */
      private $empleados;
  
      /**
       * @var array_collection 
       * 
       * @ORM\OneToMany(targetEntity="EmpleadoHorario", mappedBy="salon")
       */
      private $empleadoHorarios;
  
      // Getters &amp; Setters...
  }
  </code></pre>
  
  <p>The <strong>Empleado</strong> Entity:</p>
  
  <pre><code>/**
   * Empleado
   *
   * @ORM\Table(name="empleado")
   * @ORM\Entity
   */
  class Empleado
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="bigint", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="NONE")
       */
      private $id;
  
      /**
       * @var string
       *
       * @ORM\JoinColumn(name="salon_id", referencedColumnName="id", nullable=false)
       * @ORM\ManyToOne(targetEntity="Salon", inversedBy="empleados")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="NONE")
       */
      private $salon;
  
      // More fields...
  
      /**
       * @var array_collection 
       * 
       * @ORM\OneToMany(targetEntity="EmpleadoHorario", mappedBy="salon")
       */
      private $empleadoHorarios;
  
      // Getters &amp; setters...
  
  }
  </code></pre>
  
  <p>And finally the <strong>EmpleadoHorario</strong> Entity:</p>
  
  <pre><code>/**
   * EmpleadoHorario
   *
   * @ORM\Table(name="empleado_horario")
   * @ORM\Entity
   */
  class EmpleadoHorario
  {
      /**
       * @var \DateTime
       *
       * @ORM\Column(name="fecha", type="date", nullable=false)
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="NONE")
       */
      private $fecha;
  
      /**
       * @var string
       *
       * @ORM\JoinColumn(name="salon_id", referencedColumnName="id", nullable=false)
       * @ORM\ManyToOne(targetEntity="Salon", inversedBy="empleadoHorarios")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="NONE")
       */
      private $salon;
  
      /**
       * @var integer
       *
       * @ORM\ManyToOne(targetEntity="Empleado", inversedBy="empleadoHorarios")
       * @ORM\JoinColumns({
       *   @ORM\JoinColumn(name="salon_id", referencedColumnName="salon_id", nullable=false),
       *   @ORM\JoinColumn(name="empleado_id", referencedColumnName="id", nullable=false)
       * })
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="NONE")
       */
      private $empleado;
  
      // More fields...
  
      // Getters &amp; Setters...
  
  }
  </code></pre>
  
  <p>As I said above, the problem seems to be in the <strong>EmpleadoHorario.empleado</strong> field, which is part of a composite primary key and also composite foreing key.</p>
  
  <p>Other answers across StackOverflow.com suggest the Mapping Inheritance, but I don't even know how it works. I tried twice after  <a href="http://docs.doctrine-project.org/en/latest/reference/inheritance-mapping.html" rel="nofollow">reading this</a> but I couldn't solve my problem.</p>