提问人:floriank 提问时间:11/12/2023 更新时间:11/12/2023 访问量:25
原则:OneToOne 的级联持续不保留关联数据
Doctrine: Cascade persists for OneToOne does not persist associated data
问:
我正在尝试将新用户与用户配置文件一起保存,由于某种原因,仅保存了用户记录,但根本没有保存用户配置文件。老实说,我很难理解如何正确配置关联。我很确定单侧或双侧的 OneToOne 配置有问题,但我无法弄清楚是什么。
控制器(节选)
if ($form->isSubmitted()) {
if (!$form->isValid()) {
dd($form->getErrors(true));
}
$user = $form->getData()['user'];
$userProfile = $form->getData()['userProfile'];
// Hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $passwordHasher->hashPassword(
$user,
$user->getPassword()
);
$user->setPassword($hashedPassword);
$user->setProfile($form->getData()['userProfile']);
$userProfile->setUser($user);
$entityManager->persist($user);
$entityManager->flush();
dd('PERSISTED');
}
用户实体(摘录)
#[ORM\Id]
#[ORM\Column(type: 'uuid', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
private ?string $userId = null;
#[OneToOne(targetEntity: UserProfile::class, mappedBy: 'profile', cascade: ['all'])]
private UserProfile $profile;
用户配置文件实体(摘录)
#[ORM\Id]
#[ORM\Column(type: 'uuid', unique: true)]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
private ?string $userProfileId = null;
#[OneToOne(targetEntity: User::class, inversedBy: 'profile', cascade: ['all'])]
#[JoinColumn(name: 'userId', referencedColumnName: 'userId')]
private User $user;
#[ORM\Column(type: Types::GUID)]
#[ORM\JoinColumn(nullable: false)]
private ?string $userId = null;
答: 暂无答案
评论
User
UserProfile
cascade=all
User::setProfile()
UserProfile::setUser()
ORM\
OneToOne
JoinColumn