在评论下显示作者姓名而非作者ID

时间:2013-03-11 09:44:48

标签: php cakephp-2.2

在我的应用程序中,我有2个控制器,一个用于文章(Articles),另一个用于文章评论(Article_Comments)。他们的表格如下:

Articles
id, author_id, type_id, body, created, modified

Article_Comments
id, article_id, author_id, body, created, modified

现在在文章的视图中,我也会显示评论。但是评论没有显示该评论作者的姓名。它只显示author_id

如何更改它以显示名称而不是ID?

编辑1

这是文章视图的代码:(显示注释的位)

<?php if (!empty($article['articleComment'])):
    $i = 0;
    foreach ($article['articleComment'] as $articleComment): ?>
        <tr>
            <td colspan="2">
                Response By: <?php echo $articleComment['author_id']; ?>
            </td>
            <td>
                On: <?php echo $articleComment['created']; ?>
            </td>
        </tr>
        <tr>
            <td colspan="3"><?php echo $articleComment['comment']; ?></td>
        </tr>
    <?php endforeach; ?>
<?php endif; ?>

2 个答案:

答案 0 :(得分:0)

您需要将Article_Comments与作者的表连接起来,并将此字段放在选择参数中以获取autor的名称。

答案 1 :(得分:0)

  

我已将AuthorArticle_Comments相关联。 Article_Comment是   在Author下的$hasMany模型中定义。

需要该关系才能从Author模型中检索评论,但您需要创建相反的关系,因为您从Article_Comments获取数据。像这样:

class Article_Comments extends AppModel{
    public $belongsTo = 'Author';
}
相关问题