Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP Upload Plugin: Not uploading as attachment

I am using (or trying to) the upload plugin from Jose Gonzalez: https://github.com/josegonzalez/upload, and I want to store my image records in a separate table. I followed the readme on github, but it does not point out how to enable this feature in the add/edit views and the controller. Here is what I did so far:

app/Model/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'
      )
    );

}

app/Model/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 (the relevant part):

$this->Form->input('Image.image', array('type' => 'file', 'label' => ''));

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

the file is not being uploaded, and the record in my images table is as follows:

id | model | foreign_key | name     | image | dir  | type      | size | active
---+-------+-------------+----------+-------+------+-----------+------+--------
 1 |       |           1 | test.png |       | NULL | image/png |  814 |      1

model, image and dir should not be empty/null.

the debug output debug($this->request->data) from the add()-function is:

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

The problem is, as I said above, that the upload is not working and that the image-record is incomplete. I hope this is understandable, I really don't want to store image information in the same table as the model.

like image 273
Eike Cochu Avatar asked Nov 03 '22 17:11

Eike Cochu


1 Answers

Excuse me for the late answer, but I had the same problem and it took me a while (about 4 hours) to figure it out so here's my solution for possible future reference.

In my case, the Upload (attachment) model class looks like this (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'
        )
    );
}

My product looks like this (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
}

My database scheme:

id | model | foreign_key | name     | upload | dir  | type       | size | active
---+-------+-------------+----------+--------+------+------------+------+--------
 1 |       |           1 | test.jpg |        | NULL | image/jpeg | 5820 |      1

Now the 2 solution are in the add view of product (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');

You'll need to manually add the model by adding a hidden field to your form and you'll need to add the database field to the file field. In your case it would become:

this->Form->input('Image.image.image', array('type' => 'file', 'label' => ''));
like image 161
Mark Avatar answered Nov 09 '22 04:11

Mark