2013-01-09 78 views
1

我正在关注tutorial的应用程序,其中用户可以上传属于特定用户的文件和文件。有如下的用户和上传之间的关系HABTM:Cakephp HABTM连接表空

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

一切似乎它应该有一个例外,这是工作在创建新的upload时,uploads_users表不会更新。如果我手动插入数据,那么查看和显示数据使用它的功能。任何人都可以提出什么是错的?

这里的uploads_users表:

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

我已经改变了加上传方法一点点从教程(所以它删除上传的文件,如果保存失败的数据库),所以这里的是还有:

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

回答

1

这将是有用的看看还有什么是你的要求 - >数据。如在食谱中写的,你应该使用相关的模型结构。

看起来应该如下:

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

另外在你的方法一个“UploadFile”显示你属于关联关系,我看不到。也许你在“上传”模式中不需要它。

+0

谢谢 - 它似乎确实是缺少SharedUser行,这是我的问题的原因。 – knirirr