我正在使用(或尝试)来自 Jose Gonzalez 的上传插件:https ://github.com/josegonzalez/upload ,我想将我的图像记录存储在一个单独的表中。我遵循了 github 上的自述文件,但它没有指出如何在添加/编辑视图和控制器中启用此功能。这是我到目前为止所做的:
应用程序/模型/Image.php:
class Image extends AppModel {
public $actsAs = array(
'Upload.Upload' => array(
'image' => array(
'thumbnailSizes' => array('thumb' => '20x20')
),
),
);
public $belongsTo = array(
'Profession' => array(
'className' => 'Profession',
'foreignKey' => 'foreign_key'
)
);
}
应用程序/模型/专业.php:
class Profession extends AppModel {
public $hasMany = array(
'Image' => array(
'className' => 'Image',
'foreignKey' => 'foreign_key',
'conditions' => array(
'Image.model' => 'Profession'
)
)
);
}
app/View/Professions/add.php(相关部分):
$this->Form->input('Image.image', array('type' => 'file', 'label' => ''));
应用程序/控制器/ProfessionsController.php:
public function add() {
if ($this->request->is('post')) {
if ($this->Profession->saveAll($this->request->data)) {
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Error'));
}
}
}
文件没有上传,我images
表中的记录如下:
id | model | foreign_key | name | image | dir | type | size | active
---+-------+-------------+----------+-------+------+-----------+------+--------
1 | | 1 | test.png | | NULL | image/png | 814 | 1
模型、图像和目录不应为空/null。
-function的调试输出debug($this->request->data)
是add()
:
array(
'Profession' => array(
[...]
),
'Image' => array(
'image' => array(
'name' => 'test.png',
'type' => 'image/png',
'tmp_name' => '/Applications/MAMP/tmp/php/phpTMHMF9',
'error' => (int) 0,
'size' => (int) 1473
)
)
)
问题是,正如我上面所说,上传不起作用并且图像记录不完整。我希望这是可以理解的,我真的不想将图像信息存储在与模型相同的表中。