Symfony - Embedded Forms in Propel

I have an admin module that has an input for a file where I want to upload a file. I want to upload the file to the database as blob, since that is what I am limited to. I know this is bad practice, but I cannot add files to the file system.

I have the following diagram:

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 all the widgets and schemes in BlobForm.class.php, and I tried to insert this form into my PressPhotoForm.class.php file

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

Now, when I select a file and upload it, it seems to be added to the blob_data table, but the press_photo table blob_data_id is empty and the input widget checkbox is missing to say that there is an image.

Can anyone shed some light on how I can get blob_data_id in the press_photo table when loading?

thank

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

}

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

}

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

, -

+2

- , symfony 1/Propel, :

, - Propel 1.6 ( sfPropelORMPlugin)

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

(. )

0

Source: https://habr.com/ru/post/1793589/


All Articles