0

试图让CakePHP 上传插件工作。文件上传正常,缩略图被创建......等等,但有一些问题:

-“上传”表中的“名称”字段为空

-“上传”表中的“上传”字段为空(文档示例中的“附件”字段)

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

//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');
4

1 回答 1

0

戴夫:

-“上传”表中的“名称”字段为空

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

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

您需要给模型起别名,如下所示:

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

// Then

$this->ArticleData->AliasModelHere->save($data)
于 2012-05-07T23:58:43.847 回答