教义–如何在两个实体之间建立一对一关系 [英] Doctrine – how to set up one-to-one relationship between two entities
问题描述
我有两个表:用户和联系人
I have two tables: Users and Contacts
用户
- id
- 用户名
联系人
- id
- user_id
- 电子邮件 (我简化了结构)
- id
- user_id
- email (I have simplified the structure)
现在,如何正确设置学说实体?
Now, how to setup doctrine entities properly?
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseEntity {
/**
* @Id
* @GeneratedValue
* @Column(type="bigint")
*/
protected $id;
/**
* @ORM\Column(type="string", unique=true)
*/
protected $username;
/**
* @ORM\OneToOne(targetEntity="Contact")
* @ORM\JoinColumn(name="id", referencedColumnName="user_id", onDelete="CASCADE")
**/
protected $contact;
}
联系人实体:
/**
* @ORM\Entity
* @ORM\Table(name="contacts")
*/
class Contact extends BaseEntity {
/**
* @Id
* @GeneratedValue
* @Column(type="bigint")
*/
protected $id;
/**
* @var User
* @ORM\OneToOne(targetEntity="User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* @ORM\Column(type="string")
*/
protected $email;
}
问题是我不确定实体关系是否设置正确.
The thing is that I'm not sure whether the entity relationship is set properly.
- 我不知道如何设置是否删除
User
然后删除Contact
,但是不能这样做. - 如果我先创建
$user = new User()
,然后再创建$contact = new Contact()
,该如何加入它们?$user->contact = $contact
?在persist()
和flush()
之后会正确填充user_id
并将数据插入两个表中吗? - 我遇到错误
A new entity was found through the relationship '...\User#contact' that was not configured to cascade persist operations for entity: ...\Contact@0000000015f3aa5e000000012cd799f5. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement '...\Contact#__toString()' to get a clue.
我被卡在了我认为与我的问题#1有关的那个东西上,因此我无法测试#2.
- I don't know how to set if
User
is deleted then deleteContact
but not the other way. - If I create
$user = new User()
and then$contact = new Contact()
how to join them?$user->contact = $contact
? Will it afterpersist()
andflush()
filluser_id
correctly and insert data into both tables? - I'm getting error
A new entity was found through the relationship '...\User#contact' that was not configured to cascade persist operations for entity: ...\Contact@0000000015f3aa5e000000012cd799f5. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement '...\Contact#__toString()' to get a clue.
I'm stuck on this one which I think is related to my problem #1 and therefore I can't test #2.
我已经浏览了几个小时的文档,但是我陷入困境,却找不到任何可以指导我的真实示例……有人可以通过向我展示这些实体之间的关系的正确配置来帮助我吗?
I have been browsing docs for hours now but I got stuck and I didn't find any real example that would guide me... Can anyone help me by showing me proper configuration of these entities' relationship?
推荐答案
为了级联用户删除,以便其联系人也被删除,请在Contact
实体中将onDelete="CASCADE"
添加到
JoinColumn
属性的JoinColumn
批注(仅在删除其用户后才会删除该联系人)
In order to cascade a user deletion so its contact also gets deleted, in the Contact
entity add onDelete="CASCADE"
to the
JoinColumn
annotation of the $user
property (this will only delete a contact if its user is deleted)
/**
* @var User
* @ORM\OneToOne(targetEntity="User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $user;
如果在将联系人添加到用户之前坚持联系,则不会出现任何错误.如果您想通过直接将新的非持久性联系人分配给用户来添加新联系人,
那么您需要将cascade={"persist", "remove"}
添加到您的User
实体
If you persist the contact before adding it to the user you should not get any error. If you want to add a new contact by directly assigning the new un-persisted contact to a user,
then you'll need to add cascade={"persist", "remove"}
to your User
entity
/**
* @ORM\OneToOne(targetEntity="Contact",cascade={"persist", "remove"})
* @ORM\JoinColumn(name="id", referencedColumnName="user_id")
**/
protected $contact;
这篇关于教义–如何在两个实体之间建立一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!