如何保存包含doctrine嵌入式表单的sfForm?

时间:2012-08-01 17:48:52

标签: php symfony1 symfony-1.4 symfony-forms

我有一个基于KanoonGood模型类的学说形式(KanoonGoodForm),它看起来像这样:

class KanoonGoodForm extends BaseKanoonGoodForm
{
    public function configure()
    {
       $this->widgetSchema['count_num']->setAttribute('size', '1');
       $this->widgetSchema['price']->setAttribute('size', '1');

       $this->validatorSchema['category_id']->setOption('required', false);
       $this->validatorSchema['owner_id']->setOption('required', false);
       $this->validatorSchema['shop_id']->setOption('required', false);
       $this->validatorSchema['name']->setOption('required', false);
       $this->validatorSchema['price']->setOption('required', false);
       $this->validatorSchema['count_num']->setOption('required', false);
       $this->validatorSchema['description']->setOption('required', false);
    }
}

然后我创建了KanoonGoodsGroupForm类,以获得具有任意数量的嵌入式KanoonGoodForm表单的表单:

class KanoonGoodsGroupForm extends sfForm
{
    public function configure() 
   { 
       for ($i = 0; $i < 2; $i++) 
       {
           $this->embedForm('good_'.($i+1), new KanoonGoodForm());
       }
       $this->widgetSchema->setNameFormat('KanoonGoodsGroup[%s]');
       $this->mergePostValidator(new KanoonGoodValidatorSchema());
   }
}

在一个模块(名为good)中,当调用新动作时,我回显一个KanoonGoodsGroupForm的实例:

public function executeNew(sfWebRequest $request)
{
    $this->form = new KanoonGoodsGroupForm();
}

因为所有嵌入的表单都不需要填充,正如THIS ARTICLE所说,我做了 我自己的验证器:

class KAnoonGoodValidatorSchema extends sfValidatorSchema
{
  protected function configure($options = array(), $messages = array())
  {
    $this->addMessage('category_id', 'The category_id is required.');
    $this->addMessage('owner_id', 'The owner_id is required.');
    $this->addMessage('shop_id', 'The shop_id is required.');
    $this->addMessage('name', 'The name is required.');
    $this->addMessage('price', 'The price is required.');
    $this->addMessage('count_num', 'The count_num is required.');
    $this->addMessage('description', 'The description is required.');
  }

  protected function doClean($values)
  {
    $errorSchema = new sfValidatorErrorSchema($this);

    foreach($values as $key => $value)
    {
      $errorSchemaLocal = new sfValidatorErrorSchema($this);
      // no caption and no filename, remove the empty values
      if (!$value['name'] && !$value['price']&& !$value['count_num']&& !$value['description'])
      {
        unset($values[$key]);
      }

      // some error for this embedded-form
      if (count($errorSchemaLocal))
      {
        $errorSchema->addError($errorSchemaLocal, (string) $key);
      }
    }

    // throws the error for the main form
    if (count($errorSchema))
    {
      throw new sfValidatorErrorSchema($this, $errorSchema);
    }

    return $values;
  }
}

现在我想保存那些由用户填充的嵌入表单,我应该如何在创建操作中完成此操作?

public function executeCreate(sfWebRequest $request)
{
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $this->form = new KanoonGoodsGroupForm();
    $this->form->bind($request->getParameter('KanoonGoodsGroup'), $request->getFiles('KanoonGoodsGroup'));

    if ($this->form->isValid())
    {

         //what should I do here? to save those filled embedded forms?
    }

   $this->setTemplate('edit');
  }

3 个答案:

答案 0 :(得分:2)

当我遇到类似的问题时,我用自定义的save()方法解决了它,这是一种嵌入式表格的集合

public function save($conn = null){
    foreach ($this->getEmbeddedForms() as $key => $form) {
        $object = $form->getObject();
        $object->merge($this->getValue($key));
        $object->save($conn);
    }
}

它遍历嵌入的表单,并将其对象与组表单中的验证数据合并,然后保存它们

答案 1 :(得分:0)

你必须致电:

$this->form->save();

但是,您必须实施与saveEmbeddedForms相关的部分。

答案 2 :(得分:0)

您必须为KanoonGoodsGroupForm实现保存方法,如下所示:

public function save() {
    $this->updateObjectEmbeddedForms($this->getTaintedValues());
    $this->saveEmbeddedForms();
  }

  public function saveEmbeddedForms($forms = null) {
    if (null === $forms) {
      $forms = $this->embeddedForms;
    }

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


  public function updateObjectEmbeddedForms($values, $forms = null) {
    if (null === $forms) {
      $forms = $this->embeddedForms;
    }

    foreach ($forms as $name => $form) {
      if (!isset($values[$name]) || !is_array($values[$name])) {
        continue;
      }

      if ($form instanceof sfFormObject) {
        $form->updateObject($values[$name]);
      } else {
        $this->updateObjectEmbeddedForms($values[$name], $form->getEmbeddedForms());
      }
    }
  }