【问题标题】:What is wrong with my form?我的表格有什么问题?
【发布时间】:2013-07-21 02:38:28
【问题描述】:

我在尝试将表单信息保存到数据库时遇到问题。即使在为所选网络中的每个剧院手动设置剧院 ID 之后,我的表格似乎也无效。 这是我的模块 actions.class.php 的相关部分:

这里是 executeCreate():

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

现在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');
}

这是输出:

感谢您的帮助

【问题讨论】:

  • 请在您的帖子中添加您的表单和对象的架构

标签: php forms doctrine symfony1 symfony-1.4


【解决方案1】:

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

  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.
    }
}

【讨论】:

  • 谢谢,我确实忘了取消设置theater_id 小部件。我从这个开始并验证了表格。我听取了您的建议,并将所有内容重新组织为 if ($form-&gt;isValid()) {} 条件。谢谢你。但是,表已经有很多记录了,并且第一个新添加的元组的 id 为 0,我不明白为什么,但我想这是另一个问题!
  • 我的显示表有一个自动递增的 id,这是该表单中的隐藏字段。我现在添加了一个剧院列表,可以只为一个剧院添加放映。这可以完美地使用foreach() 中的两行,但我似乎仍然无法让它适用于整个网络。我相信我也必须增加 foreach() 中隐藏显示 id 的值。 $form-&gt;getObject()-&gt;getId() 仅在 $showing = $form-&gt;save() 之后有效。我尝试在foreach 末尾递增,但出现错误? Integrity constraint violation: 1048 Column 'theater_id' cannot be null
  • 嗯...也许您应该在每次保存之前尝试创建新对象。如果我正确理解您的逻辑,您想填写一个表格,提交一个操作,并根据此表格创建与您找到的剧院一样多的行。请参阅我对答案的编辑,也许这会有所帮助。
  • 这太完美了,非常感谢!只有一件事:$myObject = $form-&gt;updateObject()-&gt;getObject() 应该是 $myObject = $form-&gt;updateObject()。我使用第一个时出错:Unknown record property / related component "object" on "Showing".
猜你喜欢
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多