我正在尝试使用 Symfony 2 和 Doctrine 2 创建一个小型论坛应用程序。我的 ForumTopic 实体有一个 last_post 字段(oneToOne 映射)。现在,当我坚持我的新帖子时
$em->persist($post);
我想更新我的 ForumTopic 实体,以便它的 last_post 字段引用这个新帖子。我刚刚意识到它不能用 Doctrine postPersist Listener 完成,所以我决定使用一个小技巧,并尝试:
$em->persist($post);
$em->flush();
$topic->setLastPost($post);
$em->persist($post);
$em->flush();
但它似乎没有更新我的主题表。
我还查看了http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/working-with-associations.html#transitive-persistence-cascade-operations希望它能解决通过在我的 Topic.orm.yml 文件中添加 cascade: [ 'persist' ] 来解决问题,但它也没有帮助。
谁能指出我的解决方案或示例课程?
我的论坛主题是:
FrontBundle\Entity\ForumTopic:
type: entity
table: forum_topics
id:
id:
type: integer
generator:
strategy: AUTO
fields:
title:
type: string(100)
nullable: false
slug:
type: string(100)
nullable: false
created_at:
type: datetime
nullable: false
updated_at:
type: datetime
nullable: true
update_reason:
type: text
nullable: true
oneToMany:
posts:
targetEntity: ForumPost
mappedBy: topic
manyToOne:
created_by:
targetEntity: User
inversedBy: articles
nullable: false
updated_by:
targetEntity: User
nullable: true
default: null
topic_group:
targetEntity: ForumTopicGroup
inversedBy: topics
nullable: false
oneToOne:
last_post:
targetEntity: ForumPost
nullable: true
default: null
cascade: [ persist ]
uniqueConstraint:
uniqueSlugByGroup:
columns: [ topic_group, slug ]
我的论坛帖子是:
FrontBundle\Entity\ForumPost:
type: entity
table: forum_posts
id:
id:
type: integer
generator:
strategy: AUTO
fields:
created_at:
type: datetime
nullable: false
updated_at:
type: datetime
nullable: true
update_reason:
type: string
nullable: true
text:
type: text
nullable: false
manyToOne:
created_by:
targetEntity: User
inversedBy: forum_posts
nullable: false
updated_by:
targetEntity: User
nullable: true
default: null
topic:
targetEntity: ForumTopic
inversedBy: posts