I'm mapping some data coming from the remote source into my entites. I'm using forms for this purpose. Here is a fragment of code that is responsible for mapping data onto entities:
$i = 0;
$x = 0;
foreach ($bestellerLists[0] as $bestsellerList) {
$List[$i] = new BestsellerList();
foreach ($bestsellerList['books'] as $book) {
$Book = new Book();
$bookForm = $this->bookForm;
$bookForm->setData($Book);
$bookForm->submit($book);
$List[$i]->addBook($Book);
unset($bookForm);
unset($Book);
$x = $x + 1;
}
$i = $i+ 1;
}
The problem that I cannot solve is that in the first iteration if the inner foreach
loop, everything is well, entities get mapped, the first $Book
is added to the current $List
.
However, the second interation of the inner loop errors out with:
You cannot change the data of a submitted form.
As you can see, the $bookForm
is assigned the value with $bookForm = $this->bookForm
. At the end of the current iteration I unset
$bookForm
in order to create a new one at the next iteration.
I really do not get why Symfony would complain about changing the data of a submitted form. A new empty form should be used at each iteration. Any idea what I might be missing here?