dongweng6241 2012-08-02 02:55
浏览 15
已采纳

使用cakephp查找数据时出错

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)));
  • 写回答

1条回答 默认 最新

  • douju5062 2012-08-02 03:04
    关注

    Assuming the Post model belongs to User and User has many Post, modify your code to be:

    $this->set('posts', $this->User->Post->find('all',array('conditions' => $conditions)));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路