Milesj上传器插件创建新记录而不是编辑现有记录

时间:2013-12-18 15:23:00

标签: cakephp uploader

我正在使用Miles Johnson的cakephp uploader plugin。我喜欢这个插件,我已经有了它可以上传照片模型的照片。 我遇到的问题是,当我尝试编辑现有记录时,不是替换与我正在编辑的记录相关联的照片,而是创建新记录。新记录具有所有相同的信息,但上传的新照片除外。

以下是照片模型的外观:

        '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')); ?>

这是控制器管理员编辑代码:

    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'));
}

我忽略了一些显而易见的事情吗?

干杯,保罗

1 个答案:

答案 0 :(得分:2)

编辑记录时,您必须设置此记录的ID。

$this->Form->input('id')操作

之前,在控制器中添加表单save()或设置ID