Symfony - 在 Propel 中嵌入表单

发布于 2024-10-18 04:14:49 字数 4693 浏览 6 评论 0原文

我有一个管理模块,其中有一个文件输入字段,我想在其中上传文件。 我希望将文件作为 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']
);

}

class PressPhotoForm extends 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);
}

}

谢谢

I have an admin module, that has a file input filed, where I'd like to upload a file.
I am looking to upload the file to the database as a blob, as this is what I am restricted to. I am aware this is bad practice, but I cannot add files to the filesystem.

I have the following schema:

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:                              ~

So far, I have a created all the widgets and schemas in the BlobForm.class.php and I have then tried to embed this form into my PressPhotoForm.class.php

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

Now when I select the file and upload, it does seem to be added to the blob_data table, but in the press_photo table, blob_data_id is blank and there is no checkbox on the input widget to say there is an image.

Would someone be able to shed some light on how I can get the blob_data_id into the press_photo table on upload?

Thanks

EDIT:

Here are my forms:

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']
);

}

class PressPhotoForm extends 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);
}

}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆沫 2024-10-25 04:14:49

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

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);
  }

希望这对某人有帮助

Ok, so my answer was i needed to override

doSave()

as the saveEmbeddedForms call is after the save() method.

So I just swapped them around in my method:

  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);
  }

Hope this helps someone

不即不离 2024-10-25 04:14:49

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

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

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

(请参阅文档

If anyone is still unfortunate enough to have to bother with symfony 1 / Propel sites, here's your solution:

I just spent a good few hours with this and found that the easiest way is to upgrade to Propel 1.6 (via the sfPropelORMPlugin) and simply use

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

(see the documentation)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文