上传插件问题

时间:2012-05-07 02:33:48

标签: cakephp plugins file-upload cakephp-2.2

尝试让CakePHP Upload Plug-in正常工作。文件上传正常,缩略图已创建...等,但有一些问题:

- 'uploads'表中的'name'字段为空

- 'uploads'表中的'upload'字段为空(doc示例中的'attachment'字段)

- 如果我在上传模型中设置的'路径'中使用{model},它会使用'upload'作为模型文件夹 - 它应该放在相关模型的文件夹中,而不是每次上传模型

//Upload MODEL
public $actsAs = array(
    'Upload.Upload' => array(
        'photo' => array(
            'thumbnailSizes' => array(
                'xvga' => '1024x768',
                'vga' => '640x480',
                'thumb' => '80x80',
            ),
            'thumbnailMethod' => 'php',
            'path' => '{ROOT}webroot{DS}uploads{DS}{model}{DS}{field}{DS}', 
            'maxSize' => '5242880', //5MB
            'mimetypes' => array('image/jpeg', 'image/png', 'image/gif', 'image/bmp'),
            'extensions' => array('jpg', 'gif', 'png'),
        ),
    )
);

//ArticleData MODEL
public $hasMany = array(
    'Upload' => array(
        'className' => 'Upload',
        'foreignKey' => 'foreign_key',
        'conditions' => array(
            'Upload.model' => 'ArticleData',
        ),
    ),
);

//CONTROLLER
public function admin_upload() {        
    if(!empty($this->request->data)) {
        $this->loadModel('Upload');
        debug($this->request->data);
        if($this->Upload->save($this->request->data)) {
            $this->Session->setFlash('SAVED!!!!!!!!');
        } else {
            $this->Session->setFlash('NOT SAVED!!!!!!!!');
        }
    }
}

// VIEW
echo $this->Form->create('ArticleData', array('type'=>'file'));
echo $this->Form->input('Upload.model', array('type'=>'hidden', 'value'=>'ArticleData'));
echo $this->Form->input('Upload.foreign_key', array('type'=>'hidden', 'value'=>'4f93676e-347c-4e0c-8e6c-0a3cadcd7f7c'));
echo $this->Form->input('Upload.photo', array('type'=>'file'));
echo $this->Form->end('Submit');

1 个答案:

答案 0 :(得分:0)

戴夫:

  

- 'uploads'表中的'name'字段为空

这是正常的,我认为更多的是“显示”名称字段而不是“文件名”字段。该名称应存储在示例中的“照片”字段中。

  

- 如果我在上传模型中设置的'路径'中使用{model},它会使用'upload'作为模型文件夹 - 它应该放在相关模型的文件夹中,而不是每次上传模型

您需要为模型添加别名,如下所示:

//ArticleData MODEL
public $hasMany = array(
    'AliasModelHere' => array(
        'className' => 'Upload',
        'foreignKey' => 'foreign_key',
        'conditions' => array(
            'Upload.model' => 'ArticleData',
        ),
    ),
);

// Then

$this->ArticleData->AliasModelHere->save($data)