如何直接添加到联接表cakephp中?

如何直接添加到联接表cakephp中?,php,cakephp,jointable,Php,Cakephp,Jointable,当我尝试向两个类的联接表中添加一些条目时,我遇到了CakePHP的问题 这是我的联接表: posts_comments id post_id comment_id 我的帖子模型: <?php class Post extends AppModel { var $hasManyAndBelongsTo = array('Comment') } ?> 和my view.ctp: <?php echo $this->Form->create('

当我尝试向两个类的联接表中添加一些条目时,我遇到了CakePHP的问题

这是我的联接表:

posts_comments

id
post_id
comment_id
我的帖子模型:

<?php

  class Post extends AppModel {
    var $hasManyAndBelongsTo = array('Comment')
  }

?>
和my view.ctp:

<?php 
echo $this->Form->create('PostComment', array('url' => 'save', 'id' => 'save')); ?>
echo $this->Form->hidden('PostComment.PostId', array('value' => $id));
echo $this->Form->input('PostComment.CommentId', array('label' => '', 'type' => 'select', 'multiple' => 'checkbox', 'options' => $CommentName, 'selected' => $CommentId));
echo $this->Form->submit('save', array('id' => 'submit'));
echo $this->Form->end();
?>

问题是,我有多个复选框,因此我真的不知道如何轻松地检查一个复选框是否已被取消选中或选中。

如果创建另一个具有两个hasMany关系的模型,而不是一个具有HABTM的模型,则可以以更简单的方式工作

两个模型之间的hasandbelongtomy实际上是 通过hasMany和belongsTo关联的三个模型 协会

我发现这是保存和检索数据最简单的方法之一

有关更多信息:

在新车型上,您可以执行以下操作:

public addRecord($userId, $postId)
    $this->data[$this->alias]['user_id'] = $userId;
    $this->data[$this->alias]['post_id'] = $postId;

    return $this->save($this->data[$this->alias]);
}
<?php 
echo $this->Form->create('PostComment', array('url' => 'save', 'id' => 'save')); ?>
echo $this->Form->hidden('PostComment.PostId', array('value' => $id));
echo $this->Form->input('PostComment.CommentId', array('label' => '', 'type' => 'select', 'multiple' => 'checkbox', 'options' => $CommentName, 'selected' => $CommentId));
echo $this->Form->submit('save', array('id' => 'submit'));
echo $this->Form->end();
?>
$this->myObject->query("INSERT INTO blablabla")
public addRecord($userId, $postId)
    $this->data[$this->alias]['user_id'] = $userId;
    $this->data[$this->alias]['post_id'] = $postId;

    return $this->save($this->data[$this->alias]);
}