There are two strange things going on here which I think break your code.
You run the
bind()
method twice which might be resetting your values on the object.I don't think that the
getObject()
method returns the object by reference.
So when you run:
$form->getObject()->setX($val);
$form->save();
then you update the field of the object returned by the form, but then save the original object which is still bound to the form.
Try doing something like this:
$myObject = $form->updateObject()->getObject();
$myObject->setX($value);
$myObject->save();
The updateObject()
is important if you use the form to edit an existing object, not to create a new one. Without this you will get the old values of the object.
If you want to run it in a loop you can loop only the setting and saving part. So you would have something like this in your processCreateForm
:
protected function processCreateForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid()) { //You can check the validity of your form at this point.
//Create $theatersList
...
$myObject = $form->updateObject();
foreach ($theatersList as $theater) {
$myObject->setTheaterId($theater->theater_id);
$showing = $myObject->save();
//Do something with $showing
}
} else {
//Print the errors.
}
}
Using this code you can unset the widget for theatre_id
in your form as it should not be set by the user and does not have to be part of the form validation.
EDIT
Some changes to the code:
protected function processCreateForm(sfWebRequest $request, sfForm $form)
{
$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
if ($form->isValid()) { //You can check the validity of your form at this point.
//Create $theatersList
...
$myObject = $form->updateObject();
$myObjectVars = $myObject->toArray();
foreach ($theatersList as $theater) {
$myNewObject = new SomeClass();
$myNewObject->fromArray($myObjectVars);
$myNewObject->setTheaterId($theater->theater_id);
$showing = $myNewObject->save();
//Do something with $showing
$myNewObject->free();
unset($myNewObject);
}
} else {
//Print the errors.
}
}