CakePhp 模型关系不提供反向引用
我的模型关系是按以下方式设置的
class Post extends AppModel {
var $name = 'Post';
var $displayField = 'title';
var $hasMany = array('Comment');
var $belongsTo = array('User');
}
class User extends AppModel {
var $name = 'User';
var $displayField = 'username';
var $hasMany = array('Post', 'Comment');
}
class Comment extends AppModel {
var $name = 'Comment';
var $displayField = 'id';
var $belongsTo = array('User', 'Post');
}
目前,当我尝试在帖子视图的评论部分引用用户名时, 它错误地指出无效索引,经过进一步调查,我注意到我的模型不包括用户信息作为我的评论的反向参考,仅帖子中包含用户信息,
结果数组如下所示:
Array
(
[Post] => Array
(
[id] => 1
[title] => test post
[user_id] => 1
[body] => this is a test post
[date_posted] => 0000-00-00 00:00:00
[url_slug] => this-is-a-test-post
)
[User] => Array
(
[id] => 1
[username] => admin
[password] => e7b9f7bc09beee85947ef987d7df49df136c7c38
[first_name] => Chris
[last_name] => McGrath
[roles] => Admin
[email] => [email protected]
[last_login] => 0000-00-00 00:00:00
[member_since] => 0000-00-00 00:00:00
[FacebookProfile] =>
[TwitterUserName] =>
)
[Comment] => Array
(
[0] => Array
(
[id] => 5
[post_id] => 1
[user_id] => 1 //expecting this to pull user model for each
[comment] => test comment
)
)
)
我的问题是这是由我在模型关联中做错的事情引起的,这是模型框架的限制还是我应该在这种类型的实例中从用户模型中手动获取用户
currently my model relation ships are set up in the following fashion
class Post extends AppModel {
var $name = 'Post';
var $displayField = 'title';
var $hasMany = array('Comment');
var $belongsTo = array('User');
}
class User extends AppModel {
var $name = 'User';
var $displayField = 'username';
var $hasMany = array('Post', 'Comment');
}
class Comment extends AppModel {
var $name = 'Comment';
var $displayField = 'id';
var $belongsTo = array('User', 'Post');
}
when i am trying to refer to the username in the comments section on my posts view
it errors out saying invalid index and upon further inestigation i have noticed that my model is not including the user info as a back reference for my comments only the user information is being included for the posts
the resulting array looks like this:
Array
(
[Post] => Array
(
[id] => 1
[title] => test post
[user_id] => 1
[body] => this is a test post
[date_posted] => 0000-00-00 00:00:00
[url_slug] => this-is-a-test-post
)
[User] => Array
(
[id] => 1
[username] => admin
[password] => e7b9f7bc09beee85947ef987d7df49df136c7c38
[first_name] => Chris
[last_name] => McGrath
[roles] => Admin
[email] => [email protected]
[last_login] => 0000-00-00 00:00:00
[member_since] => 0000-00-00 00:00:00
[FacebookProfile] =>
[TwitterUserName] =>
)
[Comment] => Array
(
[0] => Array
(
[id] => 5
[post_id] => 1
[user_id] => 1 //expecting this to pull user model for each
[comment] => test comment
)
)
)
my question is is this being caused by something i am doing incorrectly in the model associations, is this a limitation of the model framework or should i be grabbing user manually from the user model in this type of instance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试做这样的事情:
编辑:
你也可以尝试这个:
Try doing something like this:
EDIT:
You can try this too:
使用 recursive = 2 从来都不是一个好主意。
http://book.cakephp.org/view/1323/Containable
using recursive = 2 is never a good idea.
http://book.cakephp.org/view/1323/Containable