基本问题:
是否可以使用Doctrine绘制关联图,该Doctrine不引用主键,而仅引用唯一键?
扩展版本:
我有一个实体(Participation
(,可以引用2个其他实体(DropoutCause
和DischargeType
(。根据这种组合,基于数据库中的另一个(第四个(表(DropoutScenario
(,隐含了一些其他属性。因为两个被引用的实体中的任何一个都可能为null,所以我无法在第4个表中将它们声明为主键,而只能声明为唯一键。
问题是,只有当我试图用条令绘制时,我才会出现错误:
上的主键id缺少值应用程序\实体\培训\辍学场景
我是不是做错了什么,或者这根本不可能是教条造成的?如果没有,有没有更好的解决方案我可以做到这一点?
我已经搜索了很长时间,并挖掘了条令文件,但我根本找不到任何关于它的信息。。。
下面是我映射的剥离代码示例。
参与:
<?php
namespace ApplicationEntityTrainings;
use DoctrineORMMapping as ORM;
/**
* @ORMMappedSuperclass
*/
abstract class Participation {
/**
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMManyToOne(targetEntity="ApplicationEntityDropoutCause")
* @ORMJoinColumn(name="dropout_cause_id", referencedColumnName="id"))
*/
protected $dropoutCause;
/**
* @ORMManyToOne(targetEntity="ApplicationEntityDischargeType")
* @ORMJoinColumn(name="discharge_id", referencedColumnName="id"))
*/
protected $dischargeType;
/**
* @ORMManyToOne(targetEntity="DropoutScenario")
* @ORMJoinColumns({
* @ORMJoinColumn(name="discharge_id", referencedColumnName="discharge_id"),
* @ORMJoinColumn(name="dropout_cause_id", referencedColumnName="dropout_cause_id")
* })
*/
private $scenario;
退出场景:
<?php
namespace ApplicationEntityTrainings;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity
* @ORMTable(name="training_dropout_scenarios")
*/
class DropoutScenario {
/**
* @var int
* @ORMId
* @ORMColumn(type="integer")
* @ORMGeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORMManyToOne(targetEntity="ApplicationEntityDropoutCause")
* @ORMJoinColumn(name="dropout_cause_id", referencedColumnName="id"))
*/
protected $dropoutCause;
/**
* @ORMManyToOne(targetEntity="ApplicationEntityDischargeType")
* @ORMJoinColumn(name="discharge_id", referencedColumnName="id"))
*/
protected $dischargeType;
/** @ORMColumn(type="integer", name="dropout_cause_id") */
protected $dropoutCauseId;
/** @ORMColumn(type="integer", name="discharge_id") */
protected $dischargeTypeId;
由于我还没有得到任何答案,我今天又做了一项研究,通过Doctrine邮件列表论坛找到了我自己问题的答案。好像我只是搜索错了关键词。。。
遗憾的是,第二条原则并不支持它。真遗憾!:(
根据条令文件:https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/limitations-and-known-issues.html#join-具有非主键的列
不能使用指向非主键的联接列。教条会认为这些是主要的关键,并造成懒惰的负载代理,这可能会导致意外的结果。教条由于性能原因无法验证此项的正确性设置,但只能通过"验证架构"命令。
类似问题:是否可以引用';id';对于JoinColumn?
就像下面的参考文献一样,条令不支持这种类型的映射
http://www.doctrine-project.org/jira/browse/DDC-1269
http://www.doctrine-project.org/jira/browse/DDC-1114
我认为这会对你有所帮助。
我为这个非常古老的问题花了两分钱。
的确,条令2不支持与非主键映射列的映射关系,但它可以模拟它
OneToOne映射示例
yml格式示例:
### UserEntity.dcm.yml
UserEntity:
type: entity
repositoryClass: UserRepository
table: User
id:
id:
type: integer
nullable: false
length: 9
options:
unsigned: true
id: true
column: id
generator:
strategy: AUTO
fields:
status:
type: integer
nullable: false
length: 2
options:
fixed: false
column: status
dateOfBirth:
type: datetime
nullable: true
options:
fixed: false
column: date_of_birth
createdAt:
type: datetime
nullable: true
options:
fixed: false
column: created_at
userAvatar:
targetEntity: AvatarEntity
joinColumn:
name: id
referencedColumnName: user_id
lifecycleCallbacks: { }
### AvatarEntity.dcm.yml
User/AvatarEntity:
type: entity
repositoryClass: AvatarRepository
table: Avatar
id:
userId:
type: integer
nullable: false
length: 9
options:
unsigned: true
id: true
column: user_id
generator:
strategy: NONE
fields:
imageName:
type: string
nullable: false
length: 255
options:
fixed: false
column: image_name
lifecycleCallbacks: { }
有了这个yml,我们现在可以通过user的user_id列获得Avatar条目。通过id定义指向AvatarEntity.dcm.yml上的此列,以及UserEntity.dcm.mloneToOne关系,我们完成了这一技巧,生成的SQL将如我们所愿是正确的。