0
在我的用戶控制器中,我試圖在其配置文件中顯示給定用戶的帖子。我怎樣才能做到這一點?基本上,我在控制器的用戶嘗試訪問該帖子表從cakephp中的不同控制器中的表中選擇
這是我的表
//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>
我得到我的幾個用戶的我的個人資料功能「undefined index errors」參考每行profle.ctp
Undefined index: Post [APP/View/Users/profile.ctp, line 11]