在 Doctrine ORM 中创建文件上传模板
Posted
技术标签:
【中文标题】在 Doctrine ORM 中创建文件上传模板【英文标题】:Creating a file upload template in Doctrine ORM 【发布时间】:2009-11-08 09:11:00 【问题描述】:我使用 Doctrine 1.2 作为 Zend 框架项目的 ORM。我为文件定义了以下模型。
File:
columns:
id:
primary: true
type: integer(4)
unsigned: true
code:
type: string(255)
unique: true
notblank: true
path:
type: string(255)
notblank: true
size:
type: integer(4)
type:
type: enum
values: [file,document,image,video,audio,web,application,archive]
default: unknown
notnull: true
mimetype:
type: string(20)
notnull: true
width:
type: integer(2)
unsigned: true
height:
type: integer(2)
unsigned: true
现在是文件模型 php 类(暂时略过):
<?php
/**
* File
*
* This class has been auto-generated by the Doctrine ORM Framework
*
* @package ##PACKAGE##
* @subpackage ##SUBPACKAGE##
* @author ##NAME## <##EMAIL##>
* @version SVN: $Id: Builder.php 6365 2009-09-15 18:22:38Z jwage $
*/
class File extends BaseFile
public function setUp ( )
$this->hasMutator('file', 'setFile');
parent::setUp();
public function setFile ( $file )
global $Application;
// Configuration
$config = array();
$config['bal'] = $Application->getOption('bal');
// Check the file
if ( !empty($file['error']) )
$error = $file['error'];
switch ( $file['error'] )
case UPLOAD_ERR_INI_SIZE :
$error = 'ini_size';
break;
case UPLOAD_ERR_FORM_SIZE :
$error = 'form_size';
break;
case UPLOAD_ERR_PARTIAL :
$error = 'partial';
break;
case UPLOAD_ERR_NO_FILE :
$error = 'no_file';
break;
case UPLOAD_ERR_NO_TMP_DIR :
$error = 'no_tmp_dir';
break;
case UPLOAD_ERR_CANT_WRITE :
$error = 'cant_write';
break;
default :
$error = 'unknown';
break;
throw new Doctrine_Exception('error-application-file-' . $error);
return false;
if ( empty($file['tmp_name']) || !is_uploaded_file($file['tmp_name']) )
throw new Doctrine_Exception('error-application-file-invalid');
return false;
// Prepare config
$file_upload_path = realpath($config['bal']['files']['upload_path']) . DIRECTORY_SEPARATOR;
// Prepare file
$filename = $file['name'];
$file_old_path = $file['tmp_name'];
$file_new_path = $file_upload_path . $filename;
$exist_attempt = 0;
while ( file_exists($file_new_path) )
// File already exists
// Pump exist attempts
++$exist_attempt;
// Add the attempt to the end of the file
$file_new_path = $file_upload_path . get_filename($filename,false) . $exist_attempt . get_extension($filename);
// Move file
$success = move_uploaded_file($file_old_path, $file_new_path);
if ( !$success )
throw new Doctrine_Exception('Unable to upload the file.');
return false;
// Secure
$file_path = realpath($file_new_path);
$file_size = filesize($file_path);
$file_mimetype = get_mime_type($file_path);
$file_type = get_filetype($file_path);
// Apply
$this->path = $file_path;
$this->size = $file_size;
$this->mimetype = $file_mimetype;
$this->type = $file_type;
// Apply: Image
if ( $file_type === 'image' )
$image_dimensions = image_dimensions($file_path);
if ( !empty($image_dimensions) )
// It is not a image we can modify
$this->width = 0;
$this->height = 0;
else
$this->width = $image_dimensions['width'];
$this->height = $image_dimensions['height'];
// Done
return true;
/**
* Download the File
* @return
*/
public function download ( )
global $Application;
// File path
$file_upload_path = realpath($config['bal']['files']['upload_path']) . DIRECTORY_SEPARATOR;
$file_path = $file_upload_path . $this->file_path;
// Output result and download
become_file_download($file_path, null, null);
die();
public function postDelete ( $Event )
global $Application;
// Prepare
$Invoker = $Event->getInvoker();
// Configuration
$config = array();
$config['bal'] = $Application->getOption('bal');
// File path
$file_upload_path = realpath($config['bal']['files']['upload_path']) . DIRECTORY_SEPARATOR;
$file_path = $file_upload_path . $this->file_path;
// Delete the file
unlink($file_path);
// Done
return true;
我希望完成的是,我的模型文件中的上述自定义功能可以转换为验证器、模板或类似的东西。
所以希望我可以这样做:
File:
actAs:
BalFile:
columns:
id:
primary: true
type: integer(4)
unsigned: true
code:
type: string(255)
unique: true
notblank: true
path:
type: string(255)
notblank: true
size:
type: integer(4)
type:
type: enum
values: [file,document,image,video,audio,web,application,archive]
default: unknown
notnull: true
mimetype:
type: string(20)
notnull: true
width:
type: integer(2)
unsigned: true
height:
type: integer(2)
unsigned: true
我希望有一个验证器,如果我这样做的话就说出来
$File->setFile($_FILE['uploaded_file']);
它会提供一个验证错误,除了在所有关于自定义验证器的原则文档中,特别是在“虚拟”字段方面。
总之,我的问题是: 我该如何制作模板/扩展来移植此功能?我以前尝试过使用模板,但一天后总是放弃:/如果您能花时间移植以上内容,我将不胜感激。
【问题讨论】:
啊,ORM 简化我们生活的方式很漂亮,不是吗? 【参考方案1】:在我要求解决问题的问题中,在我想出的解决方案中,我首先消除了问题。
相反,我使用带有前缀的 SVN 外部和 Doctrine PEAR 样式模型来处理通用代码。这样就有:models/Bal/File.php,其中 Bal 是一个 svn 外部文件夹。为了验证,我决定使用静态构造函数,例如: File::createFromUpload($_FILES['myFile']) File::createFormPath($myFilePath);然后会引发异常,由应用程序业务逻辑(控制器)处理。
此处可见源代码: http://github.com/balupton/balphp/blob/master/trunk/lib/models/File.php
【讨论】:
以上是关于在 Doctrine ORM 中创建文件上传模板的主要内容,如果未能解决你的问题,请参考以下文章
在奏鸣曲类型集合中创建新行后如何添加 javascript?
Symfony Entites import - 无需处理元数据类