0

I'm having a problem when I try to save the form's information into the database. My form doesn't seem to be valid, even after manually setting the Theater's id for every theater in the chosen Network. Here's the related part of my module's actions.class.php :

Here's executeCreate():

public function executeCreate(sfWebRequest $request) {
    $this->form = $this->configuration->getForm();
    $this->showing = $this->form->getObject();
    $this->processCreateForm($request, $this->form);
    $this->setTemplate('new');
} 

and now processCreateForm():

protected function processCreateForm(sfWebRequest $request, sfForm $form) {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

    $form_name = $form->getName();
    $parameters = $request->getParameter($form_name);
    $network_id = $parameters['network_id'];

    $theaters_list = Doctrine_Query::create()
            [...]
            ->execute();

    foreach ($theaters_list as $theater) {
        $form->getObject()->setTheaterId($theater->theater_id);
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

        if ($form->isValid()) {
            $showing = $form->save();
        } else {
            foreach ($form->getErrorSchema()->getErrors() as $key => $error) {
                echo '<p>' . $key . ': ' . $error . '</p>';
            }
        }
    }
    $this->getUser()->setFlash('update_success', true);
    $this->setTemplate('new');
}

Here's the output :

Theater_id required output

Thank you for your help

4

1 回答 1

2

这里发生了两件奇怪的事情,我认为这会破坏你的代码。

  1. 您运行该bind()方法两次,这可能会重置对象上的值。

  2. 我不认为该getObject()方法通过引用返回对象。

所以当你运行时:

    $form->getObject()->setX($val);
    $form->save();

然后更新表单返回的对象的字段,然后保存仍绑定到表单的原始对象。

尝试做这样的事情:

    $myObject = $form->updateObject()->getObject();
    $myObject->setX($value);
    $myObject->save();

updateObject()如果您使用表单来编辑现有对象,而不是创建新对象,这一点很重要。没有这个,您将获得对象的旧值。

如果你想循环运行它,你可以只循环设置和保存部分。所以你会在你的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.
    }
}

使用此代码,您可以取消设置theatre_id表单中的小部件,因为它不应该由用户设置,也不必是表单验证的一部分。

编辑

对代码的一些更改:

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.
    }
}
于 2013-07-22T13:12:57.610 回答