开发者

Symfony - embedding forms in Propel

开发者 https://www.devze.com 2023-02-12 03:15 出处:网络
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

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


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


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)

0

精彩评论

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