【发布时间】:2012-08-02 10:36:58
【问题描述】:
我在尝试用 cakePHP 制作的投票系统时遇到了问题(在此处找到 cakephp is querying extra column and I can't figure out why),但我想通了,现在我遇到了另一个问题。
我正在制作一个投票系统,但我遇到的问题是只有一个用户可以对给定的帖子进行投票。例如,如果用户 1 对帖子 1 投票,然后用户 2 对帖子 1 投票,则用户 2 的投票将覆盖用户 1 的投票。
这是我的桌子
Votes
id | user_id | vote
Posts
id | title | body | created | modified | user_id | vote_total
我在设置关联时遇到问题
用户可以对许多帖子进行投票
一个帖子可以有很多票,但每个用户只有 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 上的投票功能完成的
【问题讨论】:
标签: php mysql cakephp database-relations