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.
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' => ''));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With