我試圖創建一個使用Symfony 2和Doctrine 2的小型論壇應用程序。我的ForumTopic實體有一個last_post字段(oneToOne映射)。現在當我堅持我的新職位Symfony 2 - 基於另一個表中新插入的記錄更新表
$em->persist($post);
我想更新我的ForumTopic實體,所以它的last_post字段將引用這個新的職位。我剛剛意識到,它不能與教義postPersist監聽器來完成,所以我決定用一個小黑客,並嘗試:
$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文件中添加級聯:['persist']來解決問題,但它也沒有幫助。
任何人都可以指向我的解決方案或示例類嗎?
我ForumTopic是:
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 ]
而且我ForumPost是:
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
有趣的是:在我將映射信息轉換爲註釋之後,它的功能就像魅力一樣... – GergelyPolonkai 2012-07-13 11:55:32