0

在我的应用程序中,我有 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; ?>
4

2 回答 2

0

您需要将 Article_Comments 与作者的表进行连接,并将该字段放在 select 语句中以获取作者的名称。

于 2013-03-11T10:04:28.323 回答
0

我已经链接AuthorArticle_Comments. Article_Comments 在Author下的模型中定义$hasMany

该关系是从Author模型中检索评论所必需的,但您需要创建相反的关系,因为您要从Article_Comments. 像这样的东西:

class Article_Comments extends AppModel{
    public $belongsTo = 'Author';
}
于 2013-03-11T10:30:43.130 回答