2

我对具有多对多关系的模型的嵌入形式有疑问。嵌入的表单将正确保存模型,但不会保存多对多关系。

例子:

架构.yml:

Mother:
  columns:
    name:
      type: string(80)

Color:
  columns:
    name:
      type: string(80)

Child:
  columns:
    mother_id:
      type: integer
    name:
      type: string(80)
  relations:
    Mother:
      class: Mother
      local: mother_id
      foreign: id
      type: one
      onDelete: cascade
      foreignType: one
      foreignAlias: Children
    FavoriteColors:
      class: Color
      refClass: ChildColor
      local: child_id
      foreign: color_id
      onDelete: cascade
      foreignAlias: Children

ChildColor:
  columns:
    child_id:
      type: integer
    color_id:
      type: integer

然后我只修改 MotherForm.class.php:

class MotherForm extends BaseMotherForm
{
  public function configure()
  {
    $this->embedForm('child', new ChildForm($this->getObject()->getChildren()));
  }
}

和 ChildForm.class.php:

class ChildForm extends BaseChildForm
{
  public function configure()
  {
    unset($this['mother_id']);
  }
}

我用学说生成模块:

php symfony doctrine:generate-module frontend mother Mother

放一些颜色数据:

Color:
  Color_1:
    name: blue
  Color_2:
    name: red
  Color_3:
    name: green
  Color_4:
    name: purple

当我调用 /frontend_dev.php/mother/new 我可以添加一个新的,母亲和孩子的名字被更新,但最喜欢的颜色永远不会被保存......

如果我使用 phpmyadmin 添加颜色和子项之间的关系,然后 /edit 调用。然后正确的颜色在选择的多选中,但我无法编辑它。

这是 Symfony 的错误还是我应该做其他事情?

更新:如果我为模型 Child 生成模块。我可以编辑最喜欢的颜色,但表格不再嵌入......

4

1 回答 1

1

我和你有同样的问题。symfony 中的 Ticket #5867就是关于这个问题的,很多人为此浪费了很多时间。

在ticket中,有人给出了解决方案。您可以尝试它们,但我不知道为什么它们在我的情况下不起作用。

在这篇文章中找到了一个解决方案。这是一篇非常有用的帖子,它解释了此错误的原因和解决方法。

基本上,该错误是由于保存嵌入表单时,symfonysave()在嵌入对象中调用,而不是在嵌入表单中调用,并且save()在对象中不更新相关表。

要解决它,您必须覆盖//lib/form/doctrine/BaseFormDoctrine.class.php(或 Propel 等效项,但随后在以下代码中将 Doctrine 替换为 Propel):

以这种方式创建函数bindEmbeddedForms()

public function bindEmbeddedForms($embedded_forms, $values)
{
  if($this->isValid())
  {
    foreach ($embedded_forms as $name => $form)
    {
      $form->isBound = true;
      $form->values = $values[$name];

      if ($form->embeddedForms)
      {
        $this->bindEmbeddedForms($form->embeddedForms, $values[$name]);
      }
    }
  }
}

以这种方式声明bind()调用其父函数的函数:

public function bind(array $taintedValues = null, array $taintedFiles = null)
{
   parent::bind($taintedValues, $taintedFiles);
   $this->bindEmbeddedForms($this->embeddedForms, $this->getValues());
}

saveEmbeddedForms()并以这种方式覆盖:

public function saveEmbeddedForms($con = null, $forms = null)
{
  if (is_null($con))
  {
    $con = $this->getConnection();
  }

  if (is_null($forms))
  {
    $forms = $this->embeddedForms;
  }

  foreach ($forms as $key => $form)
  {
    if ($form instanceof sfFormDoctrine)
    {
      if(method_exists(new $form(), 'doSaveManyToMany'))
      {
        $form->doSaveManyToMany($con);
      }
      else
      {
        $form->getObject()->save($con);
      }
      $form->saveEmbeddedForms($con);
    }
    else
    {
      $this->saveEmbeddedForms($con, $form->getEmbeddedForms());
    }
  }
}

然后,在您的嵌入式表单类中(在帖子中它在课堂上也这样做BaseFormDoctrine,但我认为它更清洁我的方式),创建doSaveManyToMany保存您的关系的方法:

public function doSaveManyToMany($con = null)
{
  if (is_null($con))
  {
    $con = $this->getConnection();
  }

  $this->object->save($con);
  /*
  * Save the many-2-many relationship
  */
  $this->save***List($con); //Ex: $this->saveAhasBList($con)
}

有人在帖子评论中说有问题并说解决方案,但这不是我的情况。

希望它对某人有所帮助,即使我的回答是针对一个相当古老的未解决问题。

于 2012-01-06T12:04:54.453 回答