2013-01-09 83 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