cakepp的模型关联问题


Model association trouble with cakephp

我试图在cakeHP中制作的投票系统遇到了问题(在这里发现cakePHP正在查询额外的列,我不知道为什么),但我发现了这个问题,现在我又遇到了另一个问题。

我正在制作一个投票系统,但我遇到的问题是,只有一个用户可以对给定的帖子进行投票。例如,如果用户1对帖子1进行投票,然后用户2对帖子1投票,则用户2的投票将覆盖用户1的投票。

这是我的桌子

Votes
id |   user_id   | vote
Posts
id | title | body | created | modified | user_id | vote_total

我在设置关联时遇到问题

  1. 用户可以在许多帖子上投票

  2. 一个帖子可以有很多投票,但每个用户只有1个

这是我的用户型号

public $hasMany = array(
    'Posts' => array( 'className'  => 'Post'),
     'Votes' => array('className'  => 'Vote')
    );

这是在我的文章模型

 public $hasMany = array( //there can be multiple votes of the same id (references post table)
    'Votes' => array('foreignKey' => 'id')
    );

我没有选票管理员。这是通过PostsController.php 上的投票功能完成的

public $hasMany = array( //there can be multiple votes of the same id (references post table)
    'Votes' => array('foreignKey' => 'id')
    );

是错误的。应该是:

 public $hasMany = array( //there can be multiple votes of the same id (references post table)
'Votes' => array('foreignKey' => 'post_id')
);

因此,您必须在Vote模型中添加post_id。