开发者

CakePHP - Associations Problems

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.


I found out my problem, thanks to savant in the #cakephp IRC channel. My models were named wrong.. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜