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.