symfony 1.4 主义形式绑定

发布于 2024-10-21 06:46:11 字数 1932 浏览 1 评论 0原文

也许是简单的问题,但我只是错过了一些东西。

我有 2 个表,其中有 3 个相同的字段(名称/类型)。

occurrenceoccurrence_queue

Schema

Occurrence:
  tableName: occurrence
  columns:
    id: {the usual}
    date: {type: date}
    start_time: {type: time}
    end_time: {type: time}
    eid: {a forign key}
OccurrenceQueue:
  tableName: occurrence_queue
  columns:
    id: <-Same As Above->
    occurrence_id: {the usual}
    date: <-Same As Above->
    start_time: <-Same As Above->
    end_time: <-Same As Above->

然后我有一个基于 Occurrence 构建的模块,即doctrine:generate-module app *name * 发生情况

在这个模块中,我加载了一组发生情况,非常标准,所以这里是 trixy 部分。我想要做的是从 Occurrence 加载 3 个公共字段,但是当我保存表单时,我希望它保存到 OccurrenceQueue 表而不是 Occurrence< /代码>表。

像这样的形式:

DATE: *date picker*
Start Time: HH:MM
End Time: HH:MM

这是我失败的尝试:

public function executeEdit(sfWebRequest $request)
{
  $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));

  $this->forward404Unless($occurrence = Doctrine_Core::getTable('Occurrence')->find(array($request->getParameter('id'))), sprintf('Object occurrence does not exist (%s).', $request->getParameter('id')));

  $request->setParameter('occurrence_id',$occurrence->getId());

  $occurrenceQForm = new OccurrenceQueueForm($occurrenceQueue());

  $occurrenceQForm->bind($request->getParameter($occurrenceQForm->getName(),$request->getFiles($occurrenceQForm->getName()));

  if($occurrenceQForm->isValid())
  {
    $occurrenceQForm->save();
    $this->redirect("happyness");
  }
}

这永远不会验证。我假设因为字段和验证是相同的,所以都是黄金,但事实并非如此。那么问题来了,我该如何完成这个任务呢?最后的想法是 OccurrenceQueue 表将用于更新 Occurrence 表。

马哈洛的帮助!

Perhaps simple question and I am just missing something.

I have 2 tables that have 3 of the same fields(names/types).

occurrence and occurrence_queue

Schema

Occurrence:
  tableName: occurrence
  columns:
    id: {the usual}
    date: {type: date}
    start_time: {type: time}
    end_time: {type: time}
    eid: {a forign key}
OccurrenceQueue:
  tableName: occurrence_queue
  columns:
    id: <-Same As Above->
    occurrence_id: {the usual}
    date: <-Same As Above->
    start_time: <-Same As Above->
    end_time: <-Same As Above->

I then have a module That is built off Occurrence i.e. doctrine:generate-module app *name* Occurrence

In this module I load a set of Occurrences, pretty standard so here comes the trixy part. What I want to do is load the 3 common field from Occurrence but when I save the form I want it to save to the OccurrenceQueue table instead of the Occurrence table.

Form like this:

DATE: *date picker*
Start Time: HH:MM
End Time: HH:MM

Here is my failed attempt at this:

public function executeEdit(sfWebRequest $request)
{
  $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));

  $this->forward404Unless($occurrence = Doctrine_Core::getTable('Occurrence')->find(array($request->getParameter('id'))), sprintf('Object occurrence does not exist (%s).', $request->getParameter('id')));

  $request->setParameter('occurrence_id',$occurrence->getId());

  $occurrenceQForm = new OccurrenceQueueForm($occurrenceQueue());

  $occurrenceQForm->bind($request->getParameter($occurrenceQForm->getName(),$request->getFiles($occurrenceQForm->getName()));

  if($occurrenceQForm->isValid())
  {
    $occurrenceQForm->save();
    $this->redirect("happyness");
  }
}

This never validates. I assumed because the field and the validations were the same it would be all golden and what not, well not the case. So the question is, how do I accomplish this task? In the end The idea is the OccurrenceQueue table will be used to update the Occurrence table.

Mahalo for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初相遇 2024-10-28 06:46:11

问题是表单永远不会绑定ocurrence_id。当使用 sfForm 时,它使用 nameFormat ( $form->getName() 部分)来生成该字段的名称,因此为了让您工作,请像这样尝试:

public function executeEdit(sfWebRequest $request)
{
  ...
  $occurrenceQForm = new OccurrenceQueueForm($occurrenceQueue());

  $values = $request->getParameter($occurrenceQForm->getName());
  $values['occurrence_id'] = $ocurrence->getId();
  $occurrenceQForm->bind($values,$request->getFiles($occurrenceQForm->getName()));
  ...
}

这不是最好的这样做的方法,但它会起作用。我真正要做的是将 Ocurrence 按参数传递给 OcurrenceQForm,如下所示:

public function executeEdit(sfWebRequest $request)
{
  ...
  $occurrenceQForm = new OccurrenceQueueForm($occurrenceQueue(), array('ocurrence' => $ocurrence));
  ...
}

在您的 OccurrenceForm 中:

public function OccurrenceQueueForm extends ...{
    public function configure()
    {
        ...
        if($this->getOption('ocurrence') != null){
            $this->setDefault('occurrence_id', $ocurrence->getId());
        }
        ...
   }
}

The problem is that the form is never binding the ocurrence_id. When using a sfForm it uses the nameFormat (the $form->getName() part) to generate the name of that field, so for you to work try it like this:

public function executeEdit(sfWebRequest $request)
{
  ...
  $occurrenceQForm = new OccurrenceQueueForm($occurrenceQueue());

  $values = $request->getParameter($occurrenceQForm->getName());
  $values['occurrence_id'] = $ocurrence->getId();
  $occurrenceQForm->bind($values,$request->getFiles($occurrenceQForm->getName()));
  ...
}

This is not the best way to do it but it will work. What would i really do is by passing the Ocurrence by parameter to the OcurrenceQForm like this:

public function executeEdit(sfWebRequest $request)
{
  ...
  $occurrenceQForm = new OccurrenceQueueForm($occurrenceQueue(), array('ocurrence' => $ocurrence));
  ...
}

And in your OccurrenceForm:

public function OccurrenceQueueForm extends ...{
    public function configure()
    {
        ...
        if($this->getOption('ocurrence') != null){
            $this->setDefault('occurrence_id', $ocurrence->getId());
        }
        ...
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文