foscommentbundle keeps creating new entries instead of saving the old one

509 Views Asked by At

Problem:

The FOSCommentBundle saves comments but everytime I refresh the page, it create a new entry in the Thread table instead of saving on the record for that page. For example, if the page slug was http://johnson.localhost/app_dev.php/page/whats-up-baby, a new record is created everytime I reresh the page, so that my table looks like this:

enter image description here

Notice all those slugs are the same! Here's how I expect it to work:

enter image description here

But As soon as I refresh, it starts creating new entries like:

enter image description here

I'm displaying the form like this:

{% include 'FOSCommentBundle:Thread:async.html.twig' with {'id': post.id} %}

And I'm using YAML, so here are my Thread and Commment YAML FIles.

Johnson\BlogBundle\Entity\Thread:
    type: entity
    table: null
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    lifecycleCallbacks: {  }


Johnson\BlogBundle\Entity\Comment:
    type: entity
    manyToOne:
        thread:
            targetEntity: Johnson\BlogBundle\Entity\Thread
            inversedBy: comments
    table: null
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    lifecycleCallbacks: {  }

I don't know how to represent:

* @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")

in YAML. Could this be the problem?

1

There are 1 best solutions below

2
On

The problem you are facing is not related to ChangeTrackingPolicy option. Anyway if you like to set the value in YAML, you can do as:

changeTrackingPolicy : DEFERRED_EXPLICIT

Now about your problem. It seams your value of provided post.id is changing on each request. Could you try to put {{ post.id }} In your template to view the value of it. It should be the same unique identifier of your post entity(assuming you have a post entity).


Updated:

Okk, As yo mentioned, there is no problem with your post id then In short the solution would be updating the generator strategy in YAML for your thread entity like:

Docudex\Bundle\CommentBundle\Entity\Thread:
    type: entity
    table: null
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: NONE
    lifecycleCallbacks: {  }
    changeTrackingPolicy : DEFERRED_EXPLICIT

Make sure to clear all wrong thread entry first, then clear cache. Hope that will solve your problem.


Explanation:

When you set the generator strategy as AUTO, Doctrine setting new auto generated id for new thread. Though it was not suppose to happen, but its happening! I found if you create the entities within a child bundle of FOSCommentBundle the auto strategy doesn't have any problem. That's why, when I tried first time it gave me no problem. Then i was able to reproduce your problem in my environment by moving the entities out of the child bundle.


Suggestion:

Though for simple setup, you can just go with your approach, but it is a good idea to create a child bundle when you use bundles like these. That will give you more power to customize.