设置包含CakePHP中的连接表 [英] Setting up contains for a join table in CakePHP

查看:91
本文介绍了设置包含CakePHP中的连接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有以下表:

 帖子
id
title
内容
用户
id
用户名
密码
配置文件
id
user_id
firstname
lastname $ b b评论
id
post_id
user_id
内容
主题
id
标题
Topic_Posts
id
topic_id
post_id

希望关系是显而易见的!我所遇到的问题是,最后一个表是一个连接表,用于帖子和主题之间的多对多关系。



这是我的帖子,主题和Topics_Posts的模型:

  class Post extends AppModel 
{
public $ name ='Post';

public $ belongsTo ='User';

public $ hasMany = array('Comment','TopicPost');

public $ actsAs = array('Containable');
}
class Topic extends AppModel
{
public $ hasMany = array(
'TopicPost'
);
}
class TopicPost extends AppModel {
public $ belongsTo = array(
'Topic','Post'
);
}

据我所知,关系是正确的?然后在我的方法我有以下语句:

  $ post = $ this-> Post-> find first',
array('contains'=>
array(
'Comment'=> array('User'=> array('Profile')),
'User'=> array('Comment','Profile')
),
'conditions'=> array('Post.id'=> $ id)

所以基本上我的问题是如何添加主题到包含,因为我没有添加注释和用户到帖子模型,所以不知道为什么或下一步做什么...



强>因为我很困惑,如何这将工作,因为我链接到连接表,而不是实际的主题...除非我只是偏离轨道。

解决方案

使用当前设置,您可以执行此操作:

 'contains'=>数组(
'TopicPost'=> array('Topic')

或者您可以添加

  public $ hasAndBelongsToMany = array(
'Topic'=> array '=>'TopicPost')
);

Post >

然后你的包将如下所示:

 'contains'=> ; array(
'Topic'


In my application I have the following tables:

    Posts
id
title
content
    Users
id
username
password
    Profiles
id
user_id
firstname
lastname
    Comments
id
post_id
user_id
content
    Topics
id
title
    Topic_Posts
id
topic_id
post_id

Hopefully the relationships are fairly obvious! The problem I am having is that last table is a join table for the many to many relationship between Posts and Topics. I'm not entirely sure how to set this up!

Here is my Models for Posts, Topics and Topics_Posts:

class Post extends AppModel
{
    public $name = 'Post';

    public $belongsTo = 'User';

    public $hasMany = array('Comment', 'TopicPost');

    public $actsAs = array('Containable');
}
class Topic extends AppModel
{
    public $hasMany = array(
        'TopicPost'
    );
}
class TopicPost extends AppModel {
    public $belongsTo = array(
        'Topic', 'Post'
    );
}

As far as I can tell the relationships are correct? And then in my method I have the following statement:

$post = $this->Post->find('first',
            array('contain'=>
            array(
                'Comment'=>array('User'=>array('Profile')),
                'User'=>array('Comment','Profile')
            ),
            'conditions'=>array('Post.id'=>$id)));

So basically my question is how to add the Topic to the contain as I haven't had to add the Comment and User to the Post model so not sure why or what to do next...

Also how would I pull the Topics out again? As I'm confused as to how this will work considering I'm linking to the join table rather than the ACTUAL Topics... Unless I'm just way off track. Help on this would be appreciated.

解决方案

With you current setup you can do this:

'contain' => array(
    'TopicPost' => array('Topic')
)

Or you can add

public $hasAndBelongsToMany = array(
    'Topic' => array('with' => 'TopicPost')
);

to the Post model.

And then your contain will look like this:

'contain' => array(
    'Topic'
)

这篇关于设置包含CakePHP中的连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆