我有User登录注册页面。
现在在同一个页面上我还有一个表格,即UserInterests。
UserProfile现在我有 PostPersist 函数,它在用户被持久化后创建新的
现在 UserProfile 与用户 ID 相关联,并UserInterests与UserProfile ID
现在客户希望在同一用户页面上使用 UserInterests,但我遇到的问题是尚未创建 UserProfile。现在怎么能坚持下去。有没有什么办法
我有User登录注册页面。
现在在同一个页面上我还有一个表格,即UserInterests。
UserProfile现在我有 PostPersist 函数,它在用户被持久化后创建新的
现在 UserProfile 与用户 ID 相关联,并UserInterests与UserProfile ID
现在客户希望在同一用户页面上使用 UserInterests,但我遇到的问题是尚未创建 UserProfile。现在怎么能坚持下去。有没有什么办法
我不认为你可以在冲洗之前获得身份证。
您可以在模型之间创建关联,这样 Doctrine 将在保存时处理 id,您可以使用以下内容检索您的 UserInterest:
$user->getProfile()->getInterests();
因此,您的 User 模型将具有包含您的 UserProfile 的属性:
/**
* @OneToOne(targetEntity="UserProfile")
* @JoinColumn(name="profile_id", referencedColumnName="id")
**/
private $profile;
并且您的 UserProfile 类应该有一个属性来保存 UserInterests 模型。
/**
* @OneToOne(targetEntity="UserInterests")
* @JoinColumn(name="interests_id", referencedColumnName="id")
**/
private $interests;
您现在可以创建一个空的 $userProfile 模型(将其他模型链接在一起,实际填充可以在您的 postPersist 函数中完成)和一个 $userInterests 模型,通过
$interests = new UserInterests();
// create an empty UserProfile, and fill it in your PostPersist function,
// that way it can already be used to link the User and UserInterests
$profile = new UserProfile();
$profile->setInterests($interests);
$user->setProfile($profile);
现在 Doctrine 会在持久化时填写 id,您无需担心它们。
更多信息在这里