0

当我尝试查找给定用户的帖子时(在 UsersController 中),我收到以下错误 "Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Posts.user_id' in 'where clause'" 。这是配置文件功能

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))); 

这是查询。

 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

编辑 - 我得到了查询,现在我遇到了另一个问题

在我的 profile.ctp 页面上,我收到以下错误

Invalid argument supplied for foreach() [APP/View/Users/profile.ctp, line 35]

这是第 35 行

 <?php foreach ($posts as $post): ?>

这是用户控制器中配置文件函数的语句

$this->set('posts', $this->User->Posts->find('all',array('conditions' => $conditions)));
4

1 回答 1

2

假设Post模型属于 User并且User 有很多 Post,修改你的代码为:

$this->set('posts', $this->User->Post->find('all',array('conditions' => $conditions)));
于 2012-08-02T03:04:45.593 回答