Cakephp HABTM 连接表为空

Posted

技术标签:

【中文标题】Cakephp HABTM 连接表为空【英文标题】:Cakephp HABTM join table empty 【发布时间】:2013-01-09 17:35:01 【问题描述】:

我正在关注tutorial 的应用程序,用户可以在该应用程序中上传文件并且文件属于特定用户。 Users 和 Uploads 之间的 HABTM 关系如下:

上传.php

var $hasAndBelongsToMany = array(
    'SharedUser' => array(
        'className' => 'User',
        'joinTable' => 'uploads_users',
        'foreignKey' => 'upload_id',
        'associationForeignKey' => 'user_id',
        'unique' => 'keepExisting'
    )
);

用户.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    |                |
+-----------+----------+------+-----+---------+----------------+

我对教程中的 add Upload 方法做了一些改动(所以如果数据库保存失败,它会删除上传的文件),所以也是这样:

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”中显示的belongsTo 关系。也许您在“上传”模型中不需要它。

【讨论】:

谢谢 - 看来我的问题确实是缺少 SharedUser 行。

以上是关于Cakephp HABTM 连接表为空的主要内容,如果未能解决你的问题,请参考以下文章

在 CakePHP 中检索 HABTM、HasMany...等数据比 -1 递归手动连接更好?

CakePHP 保存 HABTM 数据

CakePHP 2.5 habtm 不保存

CakePHP - 保存 HABTM

HABTM 关联未保存 - cakephp

CakePHP 如何处理带有/不带有 'id' 字段的 HABTM 表?