Milesj uploader plugin creates new record instead of editing existing record

129 views Asked by At

I'm using the cakephp uploader plugin by Miles Johnson. I like the plugin and I've got it working to be able to upload photos for a photo model. The problem I'm running into is that when I try to edit an existing record, instead of replacing the photo associated with the record I'm editing a new record is created. The new record has all the same information with the exception of the new photo that's been uploaded.

Here's how the photo model looks:

        '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',
                )
            )           
        ) 
    )

This is the photo admin edit view form:

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

This is the controller admin edit code:

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

Have I overlooked something obvious?

Cheers, Paul

1

There are 1 answers

2
kicaj On BEST ANSWER

When You editing record, You must set ID of this record.

Add to form $this->Form->input('id') or set ID in controller before save() action