我正在使用(或试图)来自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'
)
);
}
应用程序/型号/ Profession.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
model,image和dir不应为空/ null。
来自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
)
)
)
正如我上面所说,问题是上传不起作用,而且图像记录不完整。 我希望这是可以理解的,我真的不想将图像信息存储在与模型相同的表中。
答案 0 :(得分:2)
请原谅我迟到的答案,但是我遇到了同样的问题,我花了一些时间(大约4个小时)来弄清楚,所以这是我的解决方案,以备将来参考。
就我而言,上传(附件)模型类看起来像这样(app \ Model \ Upload.php)
class Upload extends AppModel {
public $actsAs = array(
'Upload.Upload' => array('upload')
);
public $belongsTo = array(
'Product' => array(
'className' => 'Product',
'foreignKey' => 'foreign_key'
)
);
}
我的产品看起来像这样(app \ Model \ Product.php)
class Product extends AppModel {
public $hasMany = array(
'Image' => array(
'className' => 'Upload',
'foreignKey' => 'foreign_key',
'conditions' => array(
'Image.model' => 'Product'
)
)
);
// rest of the code
}
我的数据库方案:
id | model | foreign_key | name | upload | dir | type | size | active
---+-------+-------------+----------+--------+------+------------+------+--------
1 | | 1 | test.jpg | | NULL | image/jpeg | 5820 | 1
现在,2解决方案位于产品的添加视图(appView \ Products \ add.ctp)
echo $this->Form->create('Product', array('type' => 'file'));
echo $this->Form->input('name');
echo $this->Form->input('Image.upload.upload', array('type' => 'file'));
echo $this->Form->input('Image.upload.model', array('type' => 'hidden', 'value' => 'Product'));
echo $this->Form->end('Save Product');
您需要通过向表单添加隐藏字段来手动添加模型,并且您需要将数据库字段添加到文件字段中。在你的情况下,它将成为:
this->Form->input('Image.image.image', array('type' => 'file', 'label' => ''));
答案 1 :(得分:0)
试试这个:
视图
echo $ this-> Form> input('Attachment.1.model',array('type'=>'hidden','value'=>'Sludge'));
现在,这对我有用。