2012-12-04 84 views
1

我有一个用户和一个帖子表。 当我保存帖子时,它通过HABTM成功保存(也在posts_users表中)。cakephp 2.x如何将数据保存在habtm连接表中?

现在我希望该帖子可以被多个用户访问。 如何在posts_users表中单独添加数据,以便post_id现在也与表users_posts中的其他user_id关联?

我曾尝试下面的代码:

<?php 
echo $this->Form->create('Post'); 
echo $this->Form->input('User.id',array('value'=>34)); 
echo $this->Form->input('Post.id',array('value'=>34)); 
echo $this->Form->end('Save Post'); 
?> 

回答

0

我想你想要的是独一无二HABTM键:

public $hasAndBelongsToMany = array(
    'Post' => array(
     'className' => 'Post', 
     'foreignKey' => 'user_id', 
     'associationForeignKey' => 'post_id', 
     'joinTable' => 'posts_users', 
     'unique' => 'keepExisting' 
    ) 
);