Symfony - 在Propel中嵌入表单

时间:2011-02-21 12:30:47

标签: symfony-1.4 symfony-forms

我有一个管理模块,它有一个文件输入字段,我想上传文件。 我希望将文件作为blob上传到数据库,因为这是我的限制。我知道这是不好的做法,但我无法将文件添加到文件系统。

我有以下架构:

press_photo:
  id:                                      ~
  blob_data_id:                            { type: INTEGER, required: true, foreignTable: blob_data, foreignReference: id, onDelete: cascade }
  name:                                    { type: VARCHAR, size: '100', required: true }
  created_at:                              ~
  updated_at:                              ~

blob_data:
  id:                                      ~
  blob_data:                               { type: BLOB, required: true }
  created_at:                              ~
  updated_at:                              ~

到目前为止,我已经在BlobForm.class.php中创建了所有小部件和模式,然后我尝试将此表单嵌入到我的PressPhotoForm.class.php中

$this->embedForm('blob_data', new BlobDataForm());

现在,当我选择文件并上传时,它似乎确实被添加到blob_data表中,但是在press_photo表中,blob_data_id是空白的,输入小部件上没有复选框表示有图像。

是否有人能够了解如何在上传时将blob_data_id导入press_photo表?

由于

编辑:

以下是我的表格:

class BlobDataForm extends BaseBlobDataForm
{
 public function configure()
 {
   parent::configure();
   $this->widgetSchema ['blob_data'] = new sfWidgetFormInputFileEditable ( array (
            'label' => '',
            'file_src' => $this->getObject()->getBlobData(),
            'edit_mode' => ! $this->isNew () && $this->getObject()->getBlobData(),
            'template' => '<div>%file%<br />%input%<br />%delete% %delete_label%</div>'
   ));

    $this->setWidget('blob_data_type_id', new sfWidgetFormPropelChoice(array('model' => 'BlobDataType')));
    //$this->widgetSchema['blob_data_id'] = new sfWidgetFormInputHidden();


    $this->validatorSchema['blob_data'] = new sfValidatorPass();
    $this->validatorSchema ['blob_data'] = new fdValidatorImageToDB(array(
        'required'=>false
    ));

    $this->validatorSchema['blob_data']->setOption('mime_types', array('image/jpg','image/jpeg','application/pdf','application/msword'));

    $this->widgetSchema->setNameFormat('article_files[%s]');

    $this->widgetSchema->setLabels(array(
           'blob_data_type_id'=>'File Type',
           'blob_data'=>'File'
    ));

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

unset(

  $this['file_extension'],
  //unsetting to hide the drop down select-list
  //$this['blob_data_type_id'],
  $this['image_width'],
  $this['image_height'],
  $this['filesize'],
  $this['created_at'],
  $this['updated_at']
);

}

类PressPhotoForm扩展了BasePressPhotoForm {

public function configure()
{
    // Execute the configure of the parent
    parent::configure();

    // Configure
    $this->configureWidgets();
    $this->configureValidators();
    //$this->embedForm('blob_data', new BlobDataForm());

    unset($this['blob_data_id'],$this['created_at'], $this['url'], $this['updated_at'], $this['image_size']);
}

protected function configureWidgets()
{
    $this->widgetSchema['description'] = new sfWidgetFormTextareaTinyMCE(array(
          'width' => 550,
          'height' => 350,
          'config' => 'theme_advanced_disable: "anchor,image,cleanup,help"',
    ));
      $subForm = new sfForm();
      for ($i = 0; $i < 1; $i++)
      {
        $blobData = new BlobData();
        $blobData->BlobData = $this->getObject();

        $form = new BlobDataForm($blobData);
        $pressPhoto = new PressPhoto();
        $subForm->embedForm($i, $form);

        $this->getObject()->setBlobDataId($blobData->getId());
      }
      $this->embedForm('blob_data', $subForm);


    $this->widgetSchema->setLabels(array(
           'blob_data'=>'File'
    ));
}

protected function configureValidators()
{

    $this->validatorSchema['name']->setOption('required', true);
    $this->validatorSchema['name']->setMessage('required', 'You must provide a name');

    $this->validatorSchema['press_photo_category_id']->setOption('required', true);
    $this->validatorSchema['press_photo_category_id']->setMessage('required', 'You must select a category');

}

public function saveEmbeddedForm($con = null, $forms = null)
{
    $dataForms = $this->getEmbeddedForm('blob_data')->getEmbeddedForms();
    foreach ($dataForms as $dataForm)
    $dataForm->getObject()->setBlobDataId($this->getObject()->getId());
    parent::saveEmbeddedForm($con, $forms);
}

}

由于

2 个答案:

答案 0 :(得分:2)

好的,所以我的回答是我需要覆盖

  

DoSave就会()

因为saveEmbeddedForms调用在save()方法之后。

所以我只是在我的方法中交换它们:

  protected function doSave($con = null)
  {
    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $this->updateObject();
    $blobData = new BlobData();
    //gets called first
    $this->saveEmbeddedForms($con);
    $this->getObject()->setBlobData($this->getEmbeddedForm('blob_data')->getObject());
    //object is saved after embedded form
    $this->getObject()->save($con);
  }

希望这有助于某人

答案 1 :(得分:0)

如果有人仍然不幸打扰symfony 1 / Propel网站,这是你的解决方案:

我花了几个小时用这个,发现最简单的方法是升级到Propel 1.6(通过sfPropelORMPlugin)并简单地使用

$this->embedRelation('BlobData', array(
    // optional (see documentation)
    'embedded_form_class' => 'BlobDataCustomForm'
));

(见the documentation