开发者

Saving a embed sfForm

开发者 https://www.devze.com 2023-02-15 18:28 出处:网络
I\'m making a form which consist in some text and uploading files. The problem is that the the file is not been saved in the db (blob field) because the client didn\'t want to so I made a UploadFileFo

I'm making a form which consist in some text and uploading files. The problem is that the the file is not been saved in the db (blob field) because the client didn't want to so I made a UploadFileForm to make it clean and do the logic of uploading and saving into the File Table (which has the path of that file). So i have:

//HistoryForm
class HistoryForm extends BaseHistoryForm
{
  public function configure()
  {
      unset($this['text']);

      $this->setWidget('text', new sfWidgetFormTextareaTinyMCE(array(
          'width'  => 550,
          'height' => 350,
          'config' => 'theme_advanced_disable: "anchor,image,cleanup,help"',)));
      $this->setValidator('text', new sfValidatorString());

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

//UploadFileForm
class UploadFileForm extends sfForm{
    public function configure() {
        $this->setWidget('file', new sfWidgetFormInputFile());
        $this->setValidator('file', new sfValidatorFile(array(
            'required' => false,
            'path' => sfConfig::get('sf_upload_dir')
        )));

        $this->setWidget('name', new sfWidgetFormInputText());
        $this->setValidator('name', new sfValidatorString(array('required' => true)));

        $this->widgetSchema->setNameFormat('uploadfile[%s]');
 开发者_StackOverflow社区   }

    public function save({data_to_be_saved}){
         //logic of saving
    }
}

The main problem is that embeding a doctrine form works perfectly, but if I want to save a non doctrine form the save method is never called (seems fine because not all sfForms have to be saved) but if I override the saveEmbeddedForms the embed sfForm isn't binded! Reading the symfony's code found out that when embeding a form what really does is appending the fields to the main widgetSchema so using the embed form is practically usless... So, what I do is making a save method in the sfForm which does the saving by getting all needed variables from parameters. This is call is made in the overrided method save of the main doctrine form:

//HistoryForm
    class HistoryForm extends BaseHistoryForm
    {
     ...
     public function save($con = null) {
      $hc = parent::save($con);

      foreach ($this->getEmbeddedForms() as $form) {
          $values = $this->getValue($form->getName());
          $form->save($hc, $values);
      }
  }

I spent all afternoon thinking and reading symfony's core code and didn't found a proper approach. So... does anyone knows (or thinks) a better approach?


The generally accepted approach to doing what I think you are trying to do is to infact not use an embedded form. Mixing doctrine form and sfForms is troublesome to say the least.

I'm assuming you're trying to associated a database record with a file?

If so, the usually way to do this is to create a filename field on the HistoryForm. make this field a sfWidgetInputFile widget. This widget will save to the file system, then overwrite the forms 'save' method to save the file's name filename field instead of the file contents.

Then add an accessor on the model or an asset helper class to get the record's associated file on the file system


Did you try this plugin? I had problems with embedding relations but after trying this out all of them were solved.

0

精彩评论

暂无评论...
验证码 换一张
取 消