如何在Doctrine2中使用级联选项让关联实体自动持久化? [英] How to use the cascade option in Doctrine2 to have associated entities automatically persisted?

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

问题描述

有人可以解释一下:

  $ user = new User(); 

/ *为什么在尝试插入数据库时​​我必须调用实体注释? * /
$ myFirstComment = new Comment();

$ user-> addComment($ myFirstComment);

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

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



我有级联。


  1. 这是否意味着如果我有50用户实体中的关系与其他
    实体在尝试
    update / insert / delete时,我必须手动调用每个关系?

  2. 为什么级联存在手动执行所有操作?

如果我必须手动调用所有关系,那么根本就使用Doctrine是很愚蠢的。 p>

我没有得到这个。任何帮助都不胜感激。



与此连接: doctrine 2,在关系存在时无法插入数据库

解决方案

要使Doctrine自动处理您的 User#comments 属性的持久性,您必须将级联设置为持续操作。



级联(持久,删除,合并,全部)选项使您能够忽略...

 code> $ EM->坚持($ myFirstComment); 

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



示例:

  / ** 
*双向 - 一对多(INVERSE SIDE)
*
* @OneToMany(targetEntity =Comment,mappedBy =author,cascade = {persist,remove})
* /
private $ comments;

阅读更多关于关联映射和级联在文本的跨持久性/级联选项一节。 p>

请记住:



原则只会检查关联的拥有方面是否有变更。 >



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




  • OneToMany关联从不拥有方。

  • 反面必须使用OneToOne,OneToMany或ManyToMany映射声明的 mappedBy 属性。 mappedBy 属性包含所有方面的关联字段的名称

  • 拥有方必须使用<$ c $ oneToOne,ManyToOne或ManyToMany映射声明的c> inversedBy 属性。 inversedBy 属性包含反向关联字段的名称。

  • ManyToOne始终是双向的拥有方

  • OneToMany始终是双向关联的反面。



此外: / p>

如果创建一个新的根实体(即 $ user = new User()),则只需要调用persist这不是由doctrine管理的(如果您正确设置了级联选项,则您不必在示例中调用 $ myFirstComment 中的持久性。



否则,如果实体没有因某种原因被分离,您只需调用flush。


Can someone explain me this:

$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?

I have cascade for that.

  1. Does this mean that if I have 50 relation in User Entity with other entities I have to call each relation manually when trying to update/insert/delete?
  2. Why does cascade exist if I have to do all manually?

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.

This is connected with this: doctrine 2, unable to insert to database when relation is present

解决方案

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

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

$em->persist($myFirstComment);

... 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" !

example:

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

Read more on Association mapping and cascade in the Transistive Persistence / Cascade Options chapter of the documentation.

Please remember:

Doctrine will only check the owning side of an association for changes.

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 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.

Furthermore:

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 ).

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆