Doctrine2 在复合主键中使用复合外键映射实体 [英] Doctrine2 Map entities with composite foreign keys in the composite primary keys
问题描述
我有一个模型有很多表,但在这种情况下我们只需要三个.
I have a model which has many tables, but in this case we only need three.
关键是 one 的复合主键也是外键(也是复合的),Symfony 抛出这个异常:
The point is that the composite primary key of one is also the foreing key (composite too) and Symfony throws this exception:
MappingException:无法将实体 'YourSomethingBundleEntityEmpleado' 与复合主键映射为另一个实体 'YourSomethingBundleEntityEmpleadoHorario#empleado'.
MappingException: It is not possible to map entity 'YourSomethingBundleEntityEmpleado' with a composite primary key as part of the primary key of another entity 'YourSomethingBundleEntityEmpleadoHorario#empleado'.
这里我解释一下关系:
1º Salon,它有一个主键ID
1º Salon, it has a primary key ID
2º Empleado,它有一个复合主键 ID, Salon_id,并且在主键中还有一个外键引用 Salon:Salon_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:它有一个复合主键 Fecha、Empleado_id、Salon_id,并且在主键中还有两个引用 Salon 的外键:Salon_id 和 Empleado: Empleado_id, 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:
沙龙实体:
/**
* Salon
*
* @ORMTable(name="salon")
* @ORMEntity
*/
class Salon
{
/**
* @var string
*
* @ORMColumn(name="id", type="string", length=50, nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
// More fields...
/**
* @var array_collection
*
* @ORMOneToMany(targetEntity="Empleado", mappedBy="salon")
*/
private $empleados;
/**
* @var array_collection
*
* @ORMOneToMany(targetEntity="EmpleadoHorario", mappedBy="salon")
*/
private $empleadoHorarios;
// Getters & Setters...
}
Empleado 实体:
/**
* Empleado
*
* @ORMTable(name="empleado")
* @ORMEntity
*/
class Empleado
{
/**
* @var integer
*
* @ORMColumn(name="id", type="bigint", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="NONE")
*/
private $id;
/**
* @var string
*
* @ORMJoinColumn(name="salon_id", referencedColumnName="id", nullable=false)
* @ORMManyToOne(targetEntity="Salon", inversedBy="empleados")
* @ORMId
* @ORMGeneratedValue(strategy="NONE")
*/
private $salon;
// More fields...
/**
* @var array_collection
*
* @ORMOneToMany(targetEntity="EmpleadoHorario", mappedBy="salon")
*/
private $empleadoHorarios;
// Getters & setters...
}
最后是 EmpleadoHorario 实体:
/**
* EmpleadoHorario
*
* @ORMTable(name="empleado_horario")
* @ORMEntity
*/
class EmpleadoHorario
{
/**
* @var DateTime
*
* @ORMColumn(name="fecha", type="date", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="NONE")
*/
private $fecha;
/**
* @var string
*
* @ORMJoinColumn(name="salon_id", referencedColumnName="id", nullable=false)
* @ORMManyToOne(targetEntity="Salon", inversedBy="empleadoHorarios")
* @ORMId
* @ORMGeneratedValue(strategy="NONE")
*/
private $salon;
/**
* @var integer
*
* @ORMManyToOne(targetEntity="Empleado", inversedBy="empleadoHorarios")
* @ORMJoinColumns({
* @ORMJoinColumn(name="salon_id", referencedColumnName="salon_id", nullable=false),
* @ORMJoinColumn(name="empleado_id", referencedColumnName="id", nullable=false)
* })
* @ORMId
* @ORMGeneratedValue(strategy="NONE")
*/
private $empleado;
// More fields...
// Getters & Setters...
}
正如我上面所说,问题似乎出在 EmpleadoHorario.empleado 字段中,它是复合主键和复合外键的一部分.
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.
StackOverflow.com 上的其他答案建议使用 Mapping Inheritance,但我什至不知道它是如何工作的.我在 阅读此内容之后尝试了两次,但我做不到解决我的问题.
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.
推荐答案
这个示例代码是我的(临时)解决方案的说明:
This sample code is illustration of my (temporary) solution :
<?php
namespace X;
use DoctrineORMMapping as Orm;
/**
* @OrmEntity
* @OrmTable(name="A")
*/
class A {
/**
* @OrmId
* @OrmColumn(name="id", type="integer")
* @OrmGeneratedValue(strategy="NONE")
*
* @var integer
*/
private $id;
/**
* @OrmId
*
* @var string
*/
private $otherId;
/**
* @OrmOneToMany(targetEntity="B", fetch="LAZY", mappedBy="a")
*
* @var array
*/
private $collectionOfB;
// getter, setter and other props/methods
}
/**
* @OrmEntity
* @OrmTable(name="B")
*/
class B {
/**
* @OrmId
* @OrmColumn(name="code")
*
* @var string
*/
private $code;
/**
* @OrmId
* @OrmColumn(name="a_id", type="integer")
*
* @var integer
*/
private $a_id;
/**
* @OrmId
* @OrmColumn(name="a_other_id")
*
* @var integer
*/
private $a_other_id;
/**
* @OrmManyToOne(targetEntity="A", fetch="LAZY", inversedBy="collectionOfB")
* @OrmJoinColumns({@OrmJoinColumn(name="a_id", referencedColumnName="id"), @OrmJoinColumn(name="a_other_id", referencedColumnName="other_id")})
*
* @var A
*/
private $a;
/**
* @OrmOneToOne(targetEntity="C", fetch="LAZY", mappedBy="b")
*
* @var C
*/
private $c;
// bla bla bla
}
/**
* @OrmEntity
* @OrmTable(name="C")
*/
class C {
/**
* @OrmId
* @OrmColumn(name="a_id", type="integer")
*
* @var integer
*/
private $a_id;
/**
* @OrmId
* @OrmColumn(name="a_other_id")
*
* @var integer
*/
private $a_other_id;
/**
* @OrmId
* @OrmColumn(name="b_code")
*
* @var string
*/
private $b_code;
/**
*
* @OrmOneToOne(targetEntity="B", fetch="LAZY", inversedBy="c")
* @OrmJoinColumns({@OrmJoinColumn(name="a_id", referencedColumnName="a_id"), @OrmJoinColumn(name="a_other_id", referencedColumnName="a_other_id"), @OrmJoinColumn(name="b_code", referencedColumnName="b_code")})
*
* @var B
*/
private $b;
// bla bla bla
}
这篇关于Doctrine2 在复合主键中使用复合外键映射实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!