CakePHP - 关联问题
我试图做到这一点,以便当我执行 $this->Topic->find('all') 时,它将提取所有关联的用户数据以及所有关联的帖子数据。问题是我现在无法做到这一点。
现在我执行 $this->User->find('all') ,它返回主题数据以及该主题内的所有帖子。我想这就是我的代码告诉它要做的事情,但这不是我想要的。请原谅我的解释。我真的不喜欢 CakePHP 关联。
以下是我的模型供您参考:
Topic.php
class Topic extends AppModel {
var $name = 'Topic';
var $actsAs = array('Containable');
var $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'topicid'
)
);
var $hasOne = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'id'
)
);
var $belongsTo = array(
'Forum' => array(
'className' => 'Forum',
'foreignKey' => 'id'
)
);
}
Post.php
class Post extends AppModel {
var $name = 'Post';
var $actsAs = array('Containable');
var $hasOne = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'id'
)
);
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'userid'
),
'Topic' => array(
'className' => 'Topic',
'foreignKey' => 'topicid'
)
);
}
Forum.php
class Forum extends AppModel {
var $name = 'Forum';
var $hasMany = array(
'Topic' => array(
'className' => 'Topic',
'foreignKey' => 'forumid'
)
);
}
User.php
class User extends AppModel {
var $name = 'User';
var $actsAs = array('Containable');
var $hasMany = array(
'ForumPost' => array(
'className' => 'ForumPost',
'foreignKey' => 'userid'
),
'ForumTopic' => array(
'className' => 'ForumTopic',
'foreignKey' => 'userid'
)
);
}
我想要它,以便我可以执行 $this->Topic->find('all'),它给了我 Topic数据,然后是主题中每个帖子的帖子数据,以及主题中每个帖子的用户数据(分别)。
感谢您的帮助。
I'm trying to make it so when I do a $this->Topic->find('all'), it will pull all the associated User data, and all the associated Post data. The problem is that I can't manage to do that right now.
Right now I do $this->User->find('all'), which returns the Topic data, and all Posts inside that Topic. I guess this is what my code tells it to do, but it's not what I want. Please forgive my explanation.. I really suck at CakePHP associations.
Here are my models for your reference:
Topic.php
class Topic extends AppModel {
var $name = 'Topic';
var $actsAs = array('Containable');
var $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'topicid'
)
);
var $hasOne = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'id'
)
);
var $belongsTo = array(
'Forum' => array(
'className' => 'Forum',
'foreignKey' => 'id'
)
);
}
Post.php
class Post extends AppModel {
var $name = 'Post';
var $actsAs = array('Containable');
var $hasOne = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'id'
)
);
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'userid'
),
'Topic' => array(
'className' => 'Topic',
'foreignKey' => 'topicid'
)
);
}
Forum.php
class Forum extends AppModel {
var $name = 'Forum';
var $hasMany = array(
'Topic' => array(
'className' => 'Topic',
'foreignKey' => 'forumid'
)
);
}
User.php
class User extends AppModel {
var $name = 'User';
var $actsAs = array('Containable');
var $hasMany = array(
'ForumPost' => array(
'className' => 'ForumPost',
'foreignKey' => 'userid'
),
'ForumTopic' => array(
'className' => 'ForumTopic',
'foreignKey' => 'userid'
)
);
}
I want it so that I can do $this->Topic->find('all'), and it gives me the Topic data, then the Post data for each Post in the Topic, and the User data for each Post in the Topic (respectively)..
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢#cakephp IRC 频道中的专家,我发现了我的问题。我的模型命名错误..:)
I found out my problem, thanks to savant in the #cakephp IRC channel. My models were named wrong.. :)