When I try to find a given users's posts (in UsersController), I get the following error "Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Posts.user_id' in 'where clause'" . Here's the profile function
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'));
}
$this->set('user', $this->User->read(null, $id)); //sends info to view.ctp.
$conditions = array('posts.user_id' => $id);
$this->set('posts', $this->User->Posts->find('all',array('conditions' => $conditions)));
} This is the query.
SQL Query: SELECT `Post`.`id`, `Post`.`title`, `Post`.`body`, `Post`.`created`, `Post`.`modified`, `Post`.`user_id` FROM `blog`.`posts` AS `Post` WHERE `posts`.`user_id` = 1
EDIT - I got the query to work, now I'm having another issue
On my profile.ctp page I get the following error
Invalid argument supplied for foreach() [APP/View/Users/profile.ctp, line 35]
This is line 35
<?php foreach ($posts as $post): ?>
Here is the statement from profile function in the UsersController
$this->set('posts', $this->User->Posts->find('all',array('conditions' => $conditions)));