我有帖子和评论,用户可以"喜欢"。我正在使用蛋糕PHP。
帖子和评论表上都有一个"喜欢"行,因为我不想在每次加载帖子/评论时重新计算喜欢。我也有一个喜欢表,其中包含ID(帖子ID,用户ID),以便我知道哪些用户已经"喜欢"了某些内容。
我想知道如何在cakePHP的模型中建立这种关系,以及如何在将新的喜欢添加到Likes表中时更新Posts.likes字段。
我已经在"喜欢"模型中设置了"属于"帖子和评论的喜欢,目前,我的LikesController.php如下所示:
public function add(){
...
if ($this->Like->save($this->request->data)) {
//like is added to Likes table, now how to add to the "parent" Post or Comment??
}
...
}
保持表原样,但向posts
和comments
表添加like_count
字段。
同时向posts
表添加comment_count
。
然后只需使用CakePHP的counterCache,它就会自动跟踪每个帖子的#个赞和评论。