在我的用户控制器中,我试图在他们的个人资料中显示给定用户的帖子。我怎样才能做到这一点?基本上,我在用户控制器中试图访问 Posts 表
这些是我的桌子
//Posts
id | body | user_id
//Users
user_id | username
这是我的用户的个人资料功能
UsersController.php
public function profile($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) { //if the user doesn't exist while on view.ctp, throw error message
throw new NotFoundException(__('Invalid user'));
}
$conditions = array('Posts.user_id' => $id);
$this->set('posts', $this->User->Posts->find('all',array('conditions' => $conditions)));
}
/Posts/profile.ctp
<table>
<?php foreach ($posts as $post): ?>
<tr><td><?php echo $post['Post']['body'];?>
<br>
<!--display who created the post -->
<?php echo $post['Post']['username']; ?>
<?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
</table>
关于profle.ctp 的每一行,我都收到了几个“未定义的索引错误”
Undefined index: Post [APP/View/Users/profile.ctp, line 11]