Cakephp Milesj uploader插件创建新记录,而不是编辑现有记录
我用的是迈尔斯·约翰逊的cakephp。我喜欢这个插件,我已经让它能够上传照片模型的照片。 我遇到的问题是,当我试图编辑现有记录时,会创建一个新记录,而不是替换与我正在编辑的记录关联的照片。除了上传的新照片外,新记录包含所有相同的信息 以下是照片模型的外观:Cakephp Milesj uploader插件创建新记录,而不是编辑现有记录,cakephp,uploader,Cakephp,Uploader,我用的是迈尔斯·约翰逊的cakephp。我喜欢这个插件,我已经让它能够上传照片模型的照片。 我遇到的问题是,当我试图编辑现有记录时,会创建一个新记录,而不是替换与我正在编辑的记录关联的照片。除了上传的新照片外,新记录包含所有相同的信息 以下是照片模型的外观: 'Uploader.Attachment' => array( 'imgPath' => array( // 'name' => '', // Name of the
'Uploader.Attachment' => array(
'imgPath' => array(
// 'name' => '', // Name of the function to use to format filenames
'baseDir' => '', // See UploaderComponent::$baseDir
'uploadDir' => 'files/uploads/', // See UploaderComponent::$uploadDir
'dbColumn' => 'imgPath', // The database column name to save the path to
'maxNameLength' => 60, // Max file name length
'overwrite' => false, // Overwrite file with same name if it exists
'stopSave' => true, // Stop the model save() if upload fails
'allowEmpty' => true, // Allow an empty file upload to continue
'transforms' => array( // What transformations to do on images: scale, resize, etc
array(
'method' => 'resize',
'width' => 50,
'height' => 50,
'append' => '_thumb',
'dbColumn' => 'imgPathThumb',
)
)
)
)
这是照片管理员编辑视图表单:
<?php echo $this->Html->script('ckeditor/ckeditor');?>
<?php echo $this->Form->create('Photo', array('type' => 'file'));?>
<fieldset>
<legend><?php echo __('Admin Edit Photo'); ?></legend>
<?php
echo $this->Form->input('title');
echo $this->Form->input('caption', array('class'=>'ckeditor'));
echo $this->Form->input('imgPath', array('type' => 'file'));
echo $this->Form->input('alt_tag');
echo $this->Form->input('type', array( 'label' => 'Choose a type of photo', 'options' => array(
'gallery'=>'gallery',
'news'=>'news',
'post'=>'post',
)));
echo $this->Form->input('active', array( 'label' => 'Make active', 'options' => array('yes'=>'yes','no'=>'no' )));
echo $this->Form->input('gallery_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
我是否忽略了一些显而易见的事情
干杯,保罗编辑此记录时,必须设置此记录的ID
添加到窗体
$this->form->input('id')
或在controller中设置id,然后再save()
action谢谢Kicaj,我将$this->form->input('id')添加到窗体中,并在没有运气的情况下重新测试。仍然会创建一个新记录,而不是替换所选记录的文件。还有其他想法吗?谢谢Kicaj,我重新考虑了你的建议,这确实解决了问题。再次感谢。在这里发布你的控制器代码。徐丁,我在上面添加了控制器编辑代码。谢谢你的控制器看起来不错。您可能忽略了视图文件。
public function admin_edit($id = null) {
$this->layout = 'admin';
if (!$this->Photo->exists($id)) {
throw new NotFoundException(__('Invalid photo'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Photo->save($this->request->data)) {
$this->Session->setFlash(__('The photo has been saved.'), 'success');
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The photo could not be saved. Please, try again.'), 'error');
}
} else {
$options = array('conditions' => array('Photo.' . $this->Photo->primaryKey => $id));
$this->request->data = $this->Photo->find('first', $options);
}
$galleries = $this->Photo->Gallery->find('list');
$this->set(compact('galleries'));
}