我有一个管理模块,它有一个文件输入文件,我想上传一个文件。我希望将文件作为 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);
}
}
谢谢