OneToMany 的 Doctrine 级联选项 [英] Doctrine Cascade Options for OneToMany

查看:22
本文介绍了OneToMany 的 Doctrine 级联选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解 Doctrine 手册的 解释 级联操作,需要有人帮助我理解简单多对一关系方面的选项.

I'm having a hard time making sense of the Doctrine manual's explanation of cascade operations and need someone to help me understand the options in terms of a simple ManyToOne relationship.

在我的应用程序中,我有一个名为 Article 的表/实体,它有一个引用名为 Topic 的表/实体中的id"字段的外键字段.

In my application, I have a table/entity named Article that has a foreign key field referencing the 'id' field in a table/entity named Topic.

创建新文章时,我从下拉菜单中选择主题.这会将一个整数插入到文章表中的topic_id"外键字段中.

When I create a new Article, I select the Topic from a dropdown menu. This inserts an integer into the 'topic_id' foreign key field in the Article table.

我在文章实体中设置了 $topic 关联,如下所示:

I have the $topic association set up in the Article entity like this:

/**
 * @ManyToOne(targetEntity="Topic")
 * @JoinColumn(name="topic_id", referencedColumnName="id", nullable=false)
 */
private $topic;

主题实体没有任何关于文章实体的往复注释.主题不关心哪些文章引用了它们,当删除引用该主题的文章时,主题不需要发生任何事情.

The Topic entity doesn't have any reciprocating annotation regarding the Article entity. Topics don't care what Articles reference them and nothing needs to happen to a Topic when an Article that references the Topic is deleted.

因为我没有在文章实体中指定级联操作,所以当我尝试创建新文章时,Doctrine 抛出错误:通过未配置为级联持久操作的关系找到了新实体.显式持久化新实体或在关系上配置级联持久操作."

Because I'm not specifying the cascade operation in the Article entity, Doctrine throws an error when I try to create a new Article: "A new entity was found through a relationship that was not configured to cascade persist operations. Explicitly persist the new entity or configure cascading persist operations on the relationship."

所以我知道我需要选择一个级联操作来包含在文章实体中,但是我怎么知道在这种情况下选择哪个操作?

So I know I need to choose a cascade operation to include in the Article entity, but how do I know which operation to choose in this situation?

通过阅读 Doctrine 手册,分离"听起来是正确的选择.但是在这里研究其他人的类似问题href="https://stackoverflow.com/questions/7467982/error-saving-onetomany-and-manytomany-relationship-with-doctrine-2">这里 让我觉得我想使用persist"代替.

From reading the Doctrine manual, "detach" sounds like the right option. But researching others' similar questions here and here makes me think I want to use "persist" instead.

任何人都可以帮助我理解持久化"、删除"、合并"和分离"在我所描述的简单多对一关系中的含义吗?

Can anyone help me understand what "persist," "remove," "merge," and "detach" mean in terms of a simple ManyToOne relationship like the one I've described?

推荐答案

在 Doctrine2 文档中9.6. 传递持久性/级联操作" 有几个示例说明您应该如何配置实体,以便当您坚持 $article,$topic 也将被坚持.在您的情况下,我建议为主题实体添加此注释:

In the Doctrine2 documentation "9.6. Transitive persistence / Cascade Operations" there are few examples of how you should configure your entities so that when you persist $article, the $topic would be also persisted. In your case I'd suggest this annotation for Topic entity:

/**
 * @OneToMany(targetEntity="Article", mappedBy="topic", cascade={"persist", "remove"})
 */
 private $articles;  

此解决方案的缺点是您必须将 $articles 集合包含到 Topic 实体中,但您可以将其设为私有而不使用 getter/setter.

The drawback of this solution is that you have to include $articles collection to Topic entity, but you can leave it private without getter/setter.

正如@kurt-krueckeberg 所提到的,您必须在创建新文章时传递真实的 Topic 实体,即:

And as @kurt-krueckeberg mentioned, you must pass the real Topic entity when creating new Article, i.e.:

$topic = $em->getRepository('EntityTopic')->find($id);
$article = new Article($topic);
$em->persist($article);
$em->flush();

// perhaps, in this case you don't even need to configure cascade operations

祝你好运!

这篇关于OneToMany 的 Doctrine 级联选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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