embedForm 保存问题 - Symfony

发布于 2024-10-11 21:34:07 字数 996 浏览 3 评论 0原文

我的架构如下:

架构:

 article:
   id:                                      ~
   title:                                   { type: VARCHAR, size: '255', required: true }
   created_at:                              { type: TIMESTAMP, required: true }
   updated_at:                              { type: TIMESTAMP, required: true }

 article_data:
   id:                                      ~
   article_data:                            { type: BLOB, required: true } 
   article_filename:                        { type: VARCHAR, size: '255'}
   article_id:                              { type: INTEGER, required: true, foreignTable: article, foreignReference: id, onDelete: cascade }

因此,在我的文章管理模块中,我想显示article_data 小部件,这是一个文件上传。

一切都很好。但是将上传的文件保存到服务器时,article_id字段为空。

有没有办法获取文章的 id 并将其保存为article_data 表的article_id?

谢谢

编辑:

我想我需要重写 saveEmbeddedForm() 方法,但我不确定我需要做什么。

有人可以帮助编写 saveEmbeddedForm() 的一些代码吗?

谢谢

I have schema like:

Schema:

 article:
   id:                                      ~
   title:                                   { type: VARCHAR, size: '255', required: true }
   created_at:                              { type: TIMESTAMP, required: true }
   updated_at:                              { type: TIMESTAMP, required: true }

 article_data:
   id:                                      ~
   article_data:                            { type: BLOB, required: true } 
   article_filename:                        { type: VARCHAR, size: '255'}
   article_id:                              { type: INTEGER, required: true, foreignTable: article, foreignReference: id, onDelete: cascade }

So, in my article admin module, I'd like to display the article_data widget, which is a file upload.

Everything is fine. But when saving the uploaded file to the server, the article_id field is null.

Is there a way i could get the id of the article and save it as the article_id of the article_data table?

Thanks

EDIT:

I think I need to override the saveEmbeddedForm() method, but I am not sure what I'd need to do.

Could someone help with some code for a saveEmbeddedForm()?

Thanks

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

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

发布评论

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

评论(1

心房敞 2024-10-18 21:34:07

我不知道 Propel,但在 Doctrine 中你可以这样做:

class ArticleForm extends BaseForm
{
  public function configure()
  {
    parent::configure();

    $form  = new sfForm();
    $datas = $this->getObject()->getDatas();

    foreach ($datas as $index => $data)
      $form->embedForm($index, new ArticleDataForm($data));

    $this->embedForm('dataForms', $form);
  }

  public function saveEmbeddedForm($con = null, $forms = null)
  {
    $dataForms = $this->getEmbeddedForm('dataForms')->getEmbeddedForms();

    foreach ($dataForms as $dataForm)
      $dataForm->getObject()->setArticle($this->getObject());

    parent::saveEmbeddedForm($con, $forms);
  }
}

I don't known Propel, but in Doctrine you could do something like this:

class ArticleForm extends BaseForm
{
  public function configure()
  {
    parent::configure();

    $form  = new sfForm();
    $datas = $this->getObject()->getDatas();

    foreach ($datas as $index => $data)
      $form->embedForm($index, new ArticleDataForm($data));

    $this->embedForm('dataForms', $form);
  }

  public function saveEmbeddedForm($con = null, $forms = null)
  {
    $dataForms = $this->getEmbeddedForm('dataForms')->getEmbeddedForms();

    foreach ($dataForms as $dataForm)
      $dataForm->getObject()->setArticle($this->getObject());

    parent::saveEmbeddedForm($con, $forms);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文