【问题标题】:Symfony - how to add embed Forms?Symfony - 如何添加嵌入表单?
【发布时间】:2012-05-18 14:35:03
【问题描述】:

我正在尝试创建一个表单 Houses 并将图像表单嵌入其中。我已经按照教程http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms

我有以下架构:

houses:
  actAs: { Timestampable: ~ }
  columns:
    name: { type: string(255), notnull: true }
    description: { type: string(5000), notnull: true }

images:
  actAs: { Timestampable: ~ }
  columns:
    url: { type: string(255), notnull: true }
    id_house: { type: integer, notnull: true }
    relations:
    houses: { local: id_house, foreign: id, foreignAlias: HousesImg}

和代码:

//lib/form/doctrine/ImagesCollectionForm

class ImagesCollectionForm extends sfForm
{
  public function configure()
  {
    if(!$house= $this->getOption('house'))
    {
      throw new InvalidArgumentException('You must provide an house');
    }

    for ($i = 0; $i < $this->getOption('size',2); $i++)
    {
      $images = new images();
      $images->house = $house;

      $form = new imagesForm($images);
      $this->embedForm($i, $form);
    }
  }
}

//lib/form/doctrine/housesForm.class.php

public function configure()
{
  $form = new ImagesCollectionForm(null, array('house' => $this->getObject(),'size'=>2));
  $this->embedForm('images', $form);
}

字段按预期显示。但是,当我按下保存按钮时,我得到一个空白页,并且数据没有保存在数据库中。

【问题讨论】:

  • 您是否尝试使用 dev crontroler (frontend_dev.php) 查看错误?
  • 你没有错字吗? $images-&gt;house = $house;$images-&gt;houses = $house;
  • 我发现了问题。在 imagesForm.class.php 中我没有添加 $this->useFields(...),并且 house_id 字段存在问题。现在它正在工作。
  • 无论如何,谢谢你的重播

标签: forms symfony1 embed symfony-1.4


【解决方案1】:

在与产品的图像关系中使用没有指定别名 所以默认情况下 symfony 查找关系名称

所以你需要改变 $images->house = $house;到 $images->houses = $house; 或者你可以设置相关的别名

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多