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も見てみました。 cascade: [ 'persist' ] を Topic.orm.yml ファイルに追加して問題を解決しましたが、それも役に立ちませんでした。
誰かが私に解決策やサンプルクラスを教えてもらえますか?
私のフォーラムのトピックは次のとおりです。
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