0

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
4

1 に答える 1

0

投稿をトピックの関連付けに設定する前に、投稿をフラッシュする必要があると考えているため、これが自分にとってより困難になっていると思います。

Doctrine はスマートなので、最初にフラッシュを呼び出さずにエンティティを永続化してアソシエーションに設定すると、フラッシュを呼び出すときに最初にそのエンティティを永続化して、協会。

これが意味することは、あなたが本当にする必要があるのはこれだけだということです:

// Assume that topic has been fetched from the DB, and post is completely new
$topic = $em->find('TopicModel', $topicId);
$post = new ForumPost();
// Fill in the post info
// Set up the association
$topic->setLastPost($post);
// And finally, persist and flush - no more effort needed
$em->persist($post);
$em->flush();

これの良い副作用は、単純に prePersist イベント リスナーを使用してスレッドの最後の投稿を更新できることです。Doctrine が他のすべてを処理します。

代わりに、論理的に従うのがもう少し簡単なアプローチが必要な場合は、Post モデルでトピックに対して自分で setLastPost を呼び出すことができます。たとえば、コンストラクターまたは setTopic() メソッドで投稿のトピックを設定する場合、そこに setLastPost 呼び出しを追加します。

このように、関連付けの一方の側で両方の処理を行うことは、比較的一般的な方法です。これにより、物事を適切に同期させることができます。関連付けの操作 を参照してください。

于 2012-07-08T06:32:27.420 に答える