Doctrine 2: Not all entities get persisted, why?

老子叫甜甜 提交于 2019-12-12 00:26:48

问题


I'm trying to persist Feedback entities in a loop. The problem is, that exactly 1 entity doesn't get persisted (no matter how many entities I want to persist at all; it's always n-1 instead of n).

To understand the following code snippet you have to know that a survey entity can contain several feedback entities (1..n relation).

Let's say if $feedbackCount = 10 then just 9 feedback entities get persisted into the database. At the end of this post you can also see the according doctrine unit of work. It shows that 10 feedback entities get added to to the survey (so the loop is working right) but just 9 feedback entities are listet at the end.

Any ideas guys?

Here's my code:

for ($count = 0; $count < $feedbackCount; $count++) {

            $feedback = new Feedback();
            $feedback->setFeedback("no feedback yet");
            $feedback->setSurvey($survey);
            // Add feedback to survey
            $survey->addFeedback($feedback);
            // Persist Feedback
            $entityManager->persist($survey); // persist($feedback) has same effect
        } // end for

    $uof = $entityManager->getUnitOfWork();
    dump($uof);

Here's the unit of work of the dump statement:

Here are the yaml entity configs of...

Feedback:

manyToOne:
    survey:
        targetEntity: App\APIBundle\Entity\Survey
        inversedBy: feedbacks
        cascade: ["persist"]
        joinColumn:
            name: surveyId
            referencedColumnName: id

Survey:

oneToMany:
    feedbacks:
        targetEntity: App\APIBundle\Entity\Feedback
        cascade: ["persist", "merge"] 
        mappedBy: survey

Update:

As asked by malcom, the following screenshot shows that within the ArrayColletion the Feedback entity(0) has the same properties as the Feedback entity(1). There is no difference. (irrelevant properties blurred)

Solution:

See my last comment.

来源:https://stackoverflow.com/questions/32920442/doctrine-2-not-all-entities-get-persisted-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!