主义级联选项为OneToMany [英] Doctrine Cascade Options for OneToMany

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

问题描述

我很难理解教条手册说明级联操作,需要有人帮助我了解一个简单的ManyToOne关系的选项。



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



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



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

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

主题实体没有关于文章实体的任何往复注释。主题不关心什么文章引用它们,当引用主题的文章被删除时,主题就不需要发生。



因为我没有指定级联在文章实体中的操作,当我尝试创建一个新的文章时,Doctrine会引发错误:通过未配置为级联持久化操作的关系找到一个新实体,明确地保留新的实体或在关系上配置级联持久性操作。



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

从阅读原则手册,分离听起来像正确的选项。但是研究他人的类似问题此处这里使我想我想使用持久而不是



任何人都可以帮助我了解坚持,删除,合并和分离的意思,就像一个简单的ManyToOne关系'描述?

解决方案

在Doctrine2文档9.6。传递持久性/级联操作几乎没有关于如何配置您的实体的示例,你坚持$ article,$ topic也会持续存在。在你的情况下,我建议主题实体注释:

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

此解决方案的缺点是您必须将$ articles集合包含在主题实体中,但您可以作为@ kurt-krueckeberg所提到的,你必须在创建新文章时通过真正的主题实体,即:

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

//也许,在这种情况下,您甚至不需要配置级联操作


$ b $祝你好运!


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.

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.

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.

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.

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?

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?

解决方案

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;  

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.

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

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

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

Good luck!

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

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