Reputation: 2807
I'm following a tutorial for an application where users may upload files and files belong to a particular user. There's a HABTM relationship between Users and Uploads as follows:
Upload.php:
var $hasAndBelongsToMany = array(
'SharedUser' => array(
'className' => 'User',
'joinTable' => 'uploads_users',
'foreignKey' => 'upload_id',
'associationForeignKey' => 'user_id',
'unique' => 'keepExisting'
)
);
User.php:
var $hasMany = array(
'Upload' => array(
'className' => 'Upload',
'foreignKey' => 'user_id',
'dependent' => false
)
);
var $hasAndBelongsToMany = array(
'SharedUpload' => array(
'className' => 'Upload',
'joinTable' => 'uploads_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'upload_id',
'unique' => true
)
);
All seems to work as it should with one exception, which is that on creating a new Upload the uploads_users table is not updated. If I insert data into it manually then the views which are meant to find and display data using it function. Can anyone suggest what is wrong?
Here's the uploads_users table:
+-----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| upload_id | char(36) | NO | | NULL | |
| user_id | char(36) | NO | | NULL | |
+-----------+----------+------+-----+---------+----------------+
I've altered the add Upload method a little from the tutorial (so it deletes an uploaded file if the database save fails), so here's that as well:
function add() {
if (!empty($this->data)) {
$this->Upload->create();
if ($this->uploadFile()) {
try {
if (!$this->Upload->saveAll($this->request->data)) {
throw new Exception('Couldn't save to database.');
}
$this->Session->setFlash(__('The upload has been saved', true));
$this->redirect(array('action' => 'index'));
}
catch (Exception $e) {
unlink(APP . 'tmp/uploads/' . $this->request->data['Upload']['id']);
$this->Session->setFlash(__('The upload could not be saved: ' . $e->getMessage(), true));
}
} else {
$this->Session->setFlash(__('The upload could not be saved.', true));
}
}
$users = $this->Upload->User->find('list');
$this->set(compact('users', 'users'));
}
function uploadFile() {
$file = $this->data['Upload']['file'];
if ($file['error'] === UPLOAD_ERR_OK) {
$id = String::uuid();
if (move_uploaded_file($file['tmp_name'], APP.'tmp/uploads'.DS.$id)) {
$this->request->data['Upload']['id'] = $id;
$this->request->data['Upload']['user_id'] = $this->Auth->user('id');
$this->request->data['Upload']['filename'] = $file['name'];
$this->request->data['Upload']['filesize'] = $file['size'];
$this->request->data['Upload']['filemime'] = $file['type'];
return true;
}
}
return false;
}
Upvotes: 1
Views: 597
Reputation: 249
It would be usefull to see what else is in your request->data. As written in cookbook you should use the related model structure.
It should look the following:
$this->request->data['Upload']['id'] = $id;
$this->request->data['Upload']['user_id'] = $this->Auth->user('id');
...
$this->request->data['SharedUser']['id'] = $this->Auth->user('id');
Also I could not see your belongsTo relation shown in your method "uploadFile". Maybe you don't need it in the "Upload" model.
Upvotes: 1