php - CakePhp 将评论与帖子相关联

标签 php mysql cakephp frameworks has-many

正在关注 THIS book 我正在尝试进行该练习以将评论与帖子相关联,但我没有得到它。这是我尝试过的:

class Comments extends AppModel {
    var $name = 'Comments';
    var $hasMany = 'Post';
    var $belongsToMany = 'User';

}

class User extends AppModel {
    var $name = 'User';
    var $hasMany = array('Post');



}

class Post extends AppModel {
    var $name = 'Post';
    var $belongsTo = array('User');
    var $hasAndBelongsToMany = array('Tag');
    var $hasMany = array('Comments');



}

最佳答案

我认为问题可能出在您的 Comments 类上。我猜不是:

var $hasMany = 'Post';

应该是:

var $belongsTo = 'Post';

因为评论也属于用户,所以我会直接删除下面的行,改为:

var $belongsTo = array('Post', 'User');

关于php - CakePhp 将评论与帖子相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22579619/

相关文章:

javascript - 带倒计时器的 PHP mysql javascript 游戏

javascript - CakePhp 将变量转换为 JS 变量

mysql - 如何从具有条件的重复日期行中仅选择一行

mysql - 从 3 个表中检索数据,其中 1 个表可能不包含值,并且第 3 个表的条件为 'where'

php - CakePHP - 无法在 View 中获取 session 变量

php - 使用密码 NO 的用户拒绝 MySQL 错误访问

php - 需要使用datatable jquery删除搜索字段

mysql - CakePHP:是否可以强制 find() 运行单个 MySQL 查询

mysql - 无法登录MySQL

mysql - cakephp中的列关联