这是我的代码,其中在posts数组中有一个user_id键,但我想显示用户名而不是它的id。我在哪里可以编写像findUserNameById($ id)这样的函数,并在视图中调用它。
<table>
<tr><td>Sr. No.</td><td>User</td><td>Post</td></tr>
<?php
$counter = 1;
foreach($topic['Post'] as $post) {
echo "<tr><td>".$counter."</td><td>".$post['user_id']."</td><td>".$post['body']."</td></tr>";
$counter++;
}
?>
</table>
这是我的阵列:
Array
(
[Topic] => Array
(
[id] => 1
[user_id] => 0
[title] => Some title
[visible] => 1
[created] => 2014-09-20 02:03:42
[modified] => 2014-09-20 22:30:45
)
[User] => Array
(
[id] =>
[username] =>
[password] =>
[full_name] =>
[role] =>
[created] =>
[modified] =>
)
[Post] => Array
(
[0] => Array
(
[id] => 1
[topic_id] => 1
[user_id] => 0
[body] => CakePHP 1
[created] => 2014-09-20 02:08:51
[modified] => 2014-09-20 02:08:51
)
[1] => Array
(
[id] => 2
[topic_id] => 1
[user_id] => 0
[body] => CakePHP 2
[created] => 2014-09-20 02:08:51
[modified] => 2014-09-20 02:08:51
)
)
)
该主题的用户有关键但在帖子中不可用,但我有id。