【问题标题】:Getting some extra information from a comment从评论中获取一些额外信息
【发布时间】:2012-09-08 10:22:32
【问题描述】:

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

所以我在数据库中创建了表格,并以这种方式链接了模型和用户/帖子/cmets:

用户模型:

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

帖子模型:

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

评论模型:

public $belongsTo = array("User", "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'
        )
    )
)

但是,我想要更多。我想知道是否有一种简单的方法可以获取有关发表评论的用户的一些额外信息(例如他的昵称和他的邮件)?有内置的方法吗?

【问题讨论】:

    标签: cakephp model


    【解决方案1】:

    有两种方法可以做到这一点。

    1- 在您的 find() 呼叫中设置 'recursive' => 2。这种方式的问题是,它会把所有来自二级关联的数据都带进来,可能太多了。

    2- 使用Containable behavior,并尝试如下操作:

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

    【讨论】:

    • 谢谢。也感谢您谈论递归,以后可能会有所帮助:)
    • 哼...你确定语法吗?因为这是我发送到视图的内容: $this->set("post", $this->Post->find("first", array("conditions" => array("Post.id" => $ id), "contain" => array("User", "Comment", "Comment.User"))));在调试中,我只有评论:/
    • 您是否将可包含行为附加到 Post 模型??
    • 我的错,输入错误...再次感谢:)
    猜你喜欢
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多