Symfony 2 - Updating a table based on newly inserted record in another table

Posted by W00d5t0ck on Stack Overflow See other posts from Stack Overflow or by W00d5t0ck
Published on 2012-07-07T18:18:52Z Indexed on 2012/07/09 9:15 UTC
Read the original article Hit count: 192

Filed under:
|

I'm trying to create a small forum application using Symfony 2 and Doctrine 2. My ForumTopic entity has a last_post field (oneToOne mapping). Now when I persist my new post with

$em->persist($post);

I want to update my ForumTopic entity so its last_post field would reference this new post. I have just realised that it cannot be done with a Doctrine postPersist Listener, so I decided to use a small hack, and tried:

$em->persist($post);
$em->flush();
$topic->setLastPost($post);
$em->persist($post);
$em->flush();

but it doesn't seem to update my topics table.

I also took a look at http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/working-with-associations.html#transitive-persistence-cascade-operations hoping it will solve the problem by adding cascade: [ 'persist' ] to my Topic.orm.yml file, but it didn't help, either.

Could anyone point me to a solution or an example class?

My ForumTopic is:

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 ]

And my ForumPost is:

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

© Stack Overflow or respective owner

Related posts about symfony-2.0

Related posts about doctrine2