【问题标题】:Milesj uploader plugin creates new record instead of editing existing recordMilesj 上传插件创建新记录而不是编辑现有记录
【发布时间】:2013-12-18 15:22:59
【问题描述】:

我正在使用 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'));
}

我是否忽略了一些明显的事情?

干杯,保罗

【问题讨论】:

  • 在此处发布您的控制器代码。
  • 徐丁,我已经在上面添加了控制器编辑代码。谢谢
  • 您的控制器看起来不错。您可能忽略了视图文件。

标签: cakephp uploader


【解决方案1】:

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

添加到表单$this-&gt;Form-&gt;input('id') 或在save() 操作之前在控制器中设置ID

【讨论】:

  • 感谢 Kicaj,我将 $this->Form->input('id') 添加到表单中,并重新测试但没有运气。仍会创建新记录,而不是替换所选记录的文件。还有其他想法吗?
  • 感谢 Kicaj,我重新​​审视了您的建议,这确实解决了它。再次感谢。
猜你喜欢
  • 2023-01-12
  • 2012-07-25
  • 2021-03-30
  • 2011-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
相关资源
最近更新 更多