本文介绍了如何使用 Doctrine2 中的级联选项让关联实体自动持久化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我解释一下:

$user = new User();

/* why do I have to call Entity Comment while trying to insert into db?  */
$myFirstComment = new Comment();

$user->addComment($myFirstComment);

$em->persist($user);
$em->persist($myFirstComment);
$em->flush();

为什么在尝试插入数据库时​​必须调用实体注释?

Why do I have to call Entity Comment while trying to insert into db?

我有级联.

  1. 这是否意味着如果我在用户实体中与其他人有 50 个关系实体我必须在尝试时手动调用每个关系更新/插入/删除?
  2. 如果我必须手动完成所有操作,为什么会存在级联?

如果我必须手动调用所有这些关系,那么使用 Doctrine 有点愚蠢.

If I have to call all that relation manually it is kind of stupid to use Doctrine at all.

我不明白这个.任何帮助表示赞赏.

I do not get this. Any help is appreciated.

这与此有关:doctrine2、存在关系时无法插入数据库

推荐答案

要让 Doctrine 自动处理 User#comments 属性的持久性,您必须将级联设置为persist"操作.

To have Doctrine automatically handle the persistence of your User#comments property you have to set cascade to the "persist" operation.

级联(persist、remove、merge、all)选项使您能够省略...

The cascade ( persist, remove , merge, all ) option gives you the ability to ommit ...

$em->persist($myFirstComment);

... 例如,如果您在双向关系的反面正确设置了它.如果您使用级联删除"删除用户实体,它还可以自动删除 User#comments

... if you set it correctly on your inverse side of a bidirectional relation for example.It can also automatically remove User#comments if you remove a User entity with cascade "remove" !

示例:

/**
 * Bidirectional - One-To-Many (INVERSE SIDE)
 *
 * @OneToMany(targetEntity="Comment", mappedBy="author", cascade={"persist", "remove"})
 */
private $comments;

Transistive Persistence/Cascade Options 文档章节.

请记住:

Doctrine 只会检查关联的拥有方是否有变化.

仅对关联的反面所做的更改将被忽略.确保更新双向关联的双方(或至少是拥有方,从 Doctrine 的角度来看)

Changes made only to the inverse side of an association are ignored. Make sure to update both sides of a bidirectional association (or at least the owning side, from Doctrine’s point of view)

  • OneToMany 关联从来都不是拥有方.
  • 反面必须使用 OneToOne、OneToMany 或 ManyToMany 映射声明的 mappedBy 属性.mappedBy 属性包含所属方关联字段的名称
  • 拥有方必须使用 OneToOne、ManyToOne 或 ManyToMany 映射声明的 inversedBy 属性.inversedBy 属性包含反向侧关联字段的名称.
  • ManyToOne 始终是双向关联的拥有方.
  • OneToMany 始终是双向关联的反面.
  • OneToMany associations are never the owning side.
  • The inverse side has to use the mappedBy attribute of the OneToOne, OneToMany, or ManyToMany mapping declaration. The mappedBy attribute contains the name of the association-field on the owning side
  • The owning side has to use the inversedBy attribute of the OneToOne, ManyToOne, or ManyToMany mapping declaration. The inversedBy attribute contains the name of the association-field on the inverse-side.
  • ManyToOne is always the owning side of a bidirectional association.
  • OneToMany is always the inverse side of a bidirectional association.

此外:

如果您创建一个新的根实体(即 $user = new User() ),该实体还没有被学说管理(并且您不必调用persist如果您正确设置了级联选项,则在您的示例中的 $myFirstComment 上).

you only have to call persist if you create a new root entity ( i.e. $user = new User() ) which is not already managed by doctrine ( and you don't have to call persist on $myFirstComment in your example if you have set the cascade option correctly ).

否则你只需要在实体由于某种原因没有被分离时调用flush.

Otherwise you only have to call flush if the entity hasn't for some reason been detached.

这篇关于如何使用 Doctrine2 中的级联选项让关联实体自动持久化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:05