2

我在根表单中保存嵌入表单的自定义值时遇到问题。

我实际上可以编辑“表现形式”,并且可以添加任意数量的“commande_wifi”。一切都很好保存。

我需要为每个“commande_wifi”定制流程(有一个 'puht' 值取决于 object() 的其他值)。我已经为此浪费了几个小时。

  1. save() 仅在根表单上调用

这是正确的!只有根表单调用了 save()。因此,如果您要运行其他逻辑,您将需要覆盖 saveEmbeddedForm 方法并在之前调用该代码。前面的过度简化:当你保存一个带有嵌入表单的表单时,它调用 $this->getObject()->save(),然后它调用 saveEmbeddedForms,对于每个嵌入表单,它调用 $form->saveEmbeddedForms() 然后调用$form->getObject()->save()。了解这一点至关重要,因为它会在以后为您省去很多麻烦。 http://jmather.com/2011/01/29/6-things-to-know-about-embedded-forms-in-symfony/

我试图覆盖 saveembededForms() 但此时失败。

class manifestationForm extends BasemanifestationForm
{
  public function configure()
  {
    $this->embedRelation('commande_wifi');

  }

  public function addNewFields($number){
    $new_commandes = new BaseForm();

    for($i=0; $i <= $number; $i+=1){
      $commande = new Commande_wifi();
      $commande->setManifestation($this->getObject());
      $commande_form = new commande_wifiForm($commande);

      $new_commandes->embedForm($i,$commande_form);
    }

    $this->embedForm('new', $new_commandes);
  }

  public function bind(array $taintedValues = null, array $taintedFiles = null){

    $new_commandes = new BaseForm();
    foreach($taintedValues['new'] as $key => $new_commande){
      $commande = new Commande_wifi();
      $commande->setManifestation($this->getObject()); 
      $commande_form = new commande_wifiForm($commande);

      $new_commandes->embedForm($key,$commande_form);
    }

    $this->embedForm('new',$new_commandes);          

    parent::bind($taintedValues, $taintedFiles);
  }

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

      if ($forms === NULL)
      {
        $forms = $this->getEmbeddedForms();
      }

      foreach ($forms as $form)
      {
        if ($form instanceof sfFormObject)
        {
          $form->saveEmbeddedForms($con);
          $form->getObject()->setPuht(99);
          $form->getObject()->save($con);
        }
        else
        {
          $this->saveEmbeddedForms($con, $form->getEmbeddedForms());
        }
        //$form->getObject()->setPuht(99)->save();

      }    
  }
}

它尽快赢得我可以访问 embedForm Object()。

有什么建议吗?

4

0 回答 0