2011-07-10 40 views
0

我在产品和属性之间有多对多关系。我在我的产品表单中使用embedRelation()来编辑产品,它是属性。属性包括导致我的问题的图像。每次保存表单时,即使未上传文件,updated_at列也会更新文件属性。Symfony:从表单中排除空值save

因此,我想在保存表单时排除空的属性。

我正在使用Symfony 1.4和Doctrine 1.2。

我在我的ProductForm.class.php中想这样的事情,但我需要一些关于如何使这项工作的输入。

感谢

class ProductForm extends BaseProductForm 
{ 
    public function configure() 
    { 
     unset($this['created_at'], $this['updated_at'], $this['id'], $this['slug']); 

     $this->embedRelation('ProductProperties'); 
    } 

    public function saveEmbeddedForms($con = null, $forms = null) 
    { 
     if (null === $forms) 
     { 
     $properties = $this->getValue('ProductProperties'); 
     $forms = $this->embeddedForms; 

     foreach($properties as $p) 
     { 
      // If property value is empty, unset from $forms['ProductProperties'] 
     } 
     } 
    } 

} 

回答

0

通过检查发布的值是否为文件解决了该问题,并且如果filename和value_delete都为空,我将从数组中取消设置。这可能不是最佳实践,但它现在可行。

解决方案基于http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms

class ProductPropertyValidatorSchema extends sfValidatorSchema 
    { 
     protected function configure($options = array(), $messages = array()) 
     { 
      // N0thing to configure 
     } 

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

     foreach($values as $key => $value) 
     { 
      $errorSchemaLocal = new sfValidatorErrorSchema($this); 

       if(array_key_exists('value_delete', $values)) 
       { 
        if(!$value && !$values['value_delete']) 
        { 
         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; 
     } 
    }