从评论中获取一些额外的信息 [英] Getting some extra information from a comment

查看:182
本文介绍了从评论中获取一些额外的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个基本的成员系统与CakePHP。他们可以发布的东西,我想添加一个评论系统。

So, I have a basic members system with CakePHP. They can post stuff and I'ld like to add a comment system.

所以我做了表中的数据库,使模型和用户/帖子/评论以这种方式链接:

So I made the table in the DB and made the models and the users / posts / comments are linked this way :

用户模型:

public $hasMany = array("Post", "Comment");

帖子模型:

public $belongsTo = "User";
public $hasMany = "Comment";

评论模型:

public $belongsTo = array("User", "Post");

它似乎工作得很好,我正确得到一个职位的评论,一个帖子:

And it seems to work pretty well, I correctly get the comment for a post, here the debug for a post :

array(
    'Post' => array(
        'id' => '25',
        'title' => 'Rocket Club',
        'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam elementum sem ac sem imperdiet cursus. Quisque venenatis pulvinar ornare. Donec rutrum, lacus vel imperdiet sagittis, metus risus interdum ante, iaculis venenatis arcu nibh et odio.',
        'image' => '25-75da9db5.jpg',
        'created' => '2012-09-07 01:40:14',
        'user_id' => '19'
    ),
    'User' => array(
        'password' => '*****',
        'id' => '19',
        'username' => 'Axiol',
        'mail' => '*****',
        'firstname' => 'Arnaud',
        'lastname' => 'Delante',
        'description' => 'C'est moi le monsieur qui est derrière ce site. Dingue hein ?',
        'local' => '4420 Saint-Nicolas, Belgique',
        'twitter' => 'Axiol',
        'facebook' => 'axiol',
        'gplus' => '100906827397700671747',
        'github' => 'Axiol',
        'website' => 'http://www.axioland.me',
        'created' => '2012-09-04 02:10:31',
        'lastlogin' => '2012-09-07 01:38:44',
        'active' => '1'
    ),
    'Comment' => array(
        (int) 0 => array(
            'id' => '1',
            'content' => 'Test test d'un joli commentaire constructif et tout pour aider la personne qui a postée l'image.',
            'post_id' => '25',
            'user_id' => '19'
        )
    )
)

但是,我想要更多。我想知道是否有一个简单的方法有一些额外的信息,有关做出评论的用户(如他的昵称和他的邮件)?是否有建立方式?

But, I'ld like more. I'ld to know if there is an easy way to have some extra informations about the user that made the comment (like his nickname and his mail) ? Is there a build in way ?

推荐答案

有两种方法。

1- set 'recursive'=>在 find()调用中调用<2 这种方法的问题是它将带来第二级关联的所有数据,这可能太多了。

1- set 'recursive' => 2 in your find() call. The problem with this approach is that it will bring all the data from the second level associations, which may be too much.

2-使用可容纳的行为,并尝试这样:

2- Use Containable behavior, and try something like this:

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

这篇关于从评论中获取一些额外的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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