I have a model which has many tables, but in this case we only need three.
The point is that the composite primary key of one is also the foreing key (composite too) and Symfony throws this exception:
MappingException: It is not possible to map entity 'Your\SomethingBundle\Entity\Empleado' with a composite primary key as part of the primary key of another entity 'Your\SomethingBundle\Entity\EmpleadoHorario#empleado'.
Here I explain the relationship:
1º Salon, it has a primary key ID
2º Empleado, it has a composite primary key ID, Salon_id and, also in the primary key, a foreing key referencing Salon: Salon_id
3º EmpleadoHorario: it has a composite primary key Fecha, Empleado_id, Salon_id and, also in the primary key, two a foreing keys referencing Salon: Salon_id, and Empleado: Empleado_id, Salon_id
All the relations has also the inverse union. Here is the code:
The Salon Entity:
/**
* 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 & Setters...
}
The Empleado Entity:
/**
* 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 & setters...
}
And finally the EmpleadoHorario Entity:
/**
* 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 & Setters...
}
As I said above, the problem seems to be in the EmpleadoHorario.empleado field, which is part of a composite primary key and also composite foreing key.
Other answers across StackOverflow.com suggest the Mapping Inheritance, but I don't even know how it works. I tried twice after reading this but I couldn't solve my problem.