教义OneToOne不正确?生成一个独特的索引 [英] Doctrine OneToOne incorrectly? generating a UNIQUE INDEX
问题描述
SchemaTool正在为OneToOne的关联生成唯一索引。我相信这是不正确的。
SchemaTool is generating unique index for associations that are OneToOne. I believe this is incorrect.
在Doctrine的协会手册页面的第6.6节显示了一个OneToOne产品的一个例子,一个货运。这将显示为生成Product表:
Section 6.6 of the Associations manual page at Doctrine shows an example of a OneToOne for a Product has one Shipping. This is shown to generate the Product table:
CREATE TABLE Product (
id INT AUTO_INCREMENT NOT NULL,
shipping_id INT DEFAULT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
但是,与我的实体相同的代码用户有一个组织,我的用户表SQL生成为
However, with the same code for my entity User has one Organisation, my User table SQL is generated as
CREATE TABLE User (
id INT AUTO_INCREMENT NOT NULL,
organisation_id INT DEFAULT NULL,
UNIQ_3B978F9FA7F43455 (organisation_id),
PRIMARY KEY(id)
) ENGINE = InnoDB;
这样可以防止我在同一个组织中添加2个用户。不正确。
This prevents me adding 2 users with the same Organisation. Not correct.
我更加尝试使用唯一的JoinColumn注释参数进行详细说明。
I additinally tried to be verbose with the unique JoinColumn annotation param.
@JoinColumn(name="organisation_id", referencedColumnName="id", unique="false")
任何想法?
谢谢
推荐答案
如果一个组织有许多用户,并且许多用户拥有一个组织,而不是一个组织,那么它不是一对一关联。
If One organisation has Many Users and Many Users have One and only one organisation then it's not a One-to-One association.
一对一协会必须是唯一的。
One-to-One associations have to be unique.
您的关联是 ManyToOne
在用户端和 OneToMany
在组织方面。
Your association is ManyToOne
on the User side and OneToMany
on the organisation side.
User.php
/**
* @ManyToOne(targetEntity="Organisation")
*/
private $organisation;
Organisation.php
Organisation.php
use Doctrine\Common\Collections\ArrayCollection;
/**
* @OneToMany(targetEntity="User", mappedBy="organisation")
*/
private $users;
function __construct() {
$this->users = new ArrayCollection();
}
这篇关于教义OneToOne不正确?生成一个独特的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!