0

问题:

FOSCommentBundle 保存评论,但每次我刷新页面时,它都会在 Thread 表中创建一个新条目,而不是保存在该页面的记录中。例如,如果页面 slug 是http://johnson.localhost/app_dev.php/page/whats-up-baby,则每次我重新刷新页面时都会创建一条新记录,因此我的表如下所示:

在此处输入图像描述

请注意,所有这些蛞蝓都是一样的!这是我期望它的工作方式:

在此处输入图像描述

但是,一旦我刷新,它就会开始创建新条目,例如:

在此处输入图像描述

我正在显示这样的表格:

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

我正在使用 YAML,所以这里是我的线程和评论 YAML 文件。

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: {  }

我不知道如何表示:

* @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")

在 YAML 中。这可能是问题吗?

4

1 回答 1

0

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.

于 2014-03-24T16:18:08.943 回答