使用外键CakePHP返回值

时间:2013-06-19 22:27:02

标签: cakephp cakephp-2.0

我目前正在添加CakePHP文档中的博客示例,并创建了一个用户表,以便将帖子分配给某个用户,并在我的帖子表中添加了一个user_id字段

我在帖子控制器中写了以下功能:

public function viewuser($user_id = null){

    if (!$this->Post->exists($user_id)) {
        throw new NotFoundException(__('Invalid user'));
    }
    $options = array('conditions' => array('Post.user_id' => $user_id));
    $this->set('posts', $this->Post->find('all', $options));
}

但是当我同时访问http://localhost/cake-app/posts/viewuser/1http://localhost/cake-app/posts/viewuser/2时,它会返回所有用户的所有帖子,而不仅仅是发布了$user_id值的用户发布的帖子。

有人可以帮我解决这个问题吗?我是CakePHP的新手,所以这可能是一个简单的修复。

编辑 - 生成的SQL如下:

1   SELECT COUNT(*) AS `count` FROM `jaredisalie`.`posts` AS `Post` WHERE `Post`.`id` = 1   
2   SELECT `Post`.`id`, `Post`.`title`, `Post`.`user_id`, `Post`.`imageurl`, `Post`.`category_id`, `Post`.`summary`, `Post`.`content`, `Post`.`created`, `Post`.`modified`, `Category`.`id`, `Category`.`title`, `User`.`id`, `User`.`username`, `User`.`name`, `User`.`password`, `User`.`role`, `User`.`created`, `User`.`modified` FROM `jaredisalie`.`posts` AS `Post` LEFT JOIN `jaredisalie`.`categories` AS `Category` ON (`Post`.`category_id` = `Category`.`id`) LEFT JOIN `jaredisalie`.`users` AS `User` ON (`Post`.`user_id` = `User`.`id`) WHERE `Post`.`user_id` = 1      
3   SELECT `Category`.`id`, `Category`.`title` FROM `jaredisalie`.`categories` AS `Category` WHERE 1 = 1        
4   SELECT `Posts`.`id`, `Posts`.`title`, `Posts`.`user_id`, `Posts`.`imageurl`, `Posts`.`category_id`, `Posts`.`summary`, `Posts`.`content`, `Posts`.`created`, `Posts`.`modified` FROM `jaredisalie`.`posts` AS `Posts` WHERE `Posts`.`category_id` IN (1, 2, 3)      
5   SELECT `Post`.`id`, `Post`.`title`, `Post`.`user_id`, `Post`.`imageurl`, `Post`.`category_id`, `Post`.`summary`, `Post`.`content`, `Post`.`created`, `Post`.`modified`, `Category`.`id`, `Category`.`title`, `User`.`id`, `User`.`username`, `User`.`name`, `User`.`password`, `User`.`role`, `User`.`created`, `User`.`modified` FROM `jaredisalie`.`posts` AS `Post` LEFT JOIN `jaredisalie`.`categories` AS `Category` ON (`Post`.`category_id` = `Category`.`id`) LEFT JOIN `jaredisalie`.`users` AS `User` ON (`Post`.`user_id` = `User`.`id`) WHERE 1 = 1 ORDER BY `Post`.`modified` desc LIMIT 20        
6   SELECT COUNT(*) AS `count` FROM `jaredisalie`.`posts` AS `Post` LEFT JOIN `jaredisalie`.`categories` AS `Category` ON (`Post`.`category_id` = `Category`.`id`) LEFT JOIN `jaredisalie`.`users` AS `User` ON (`Post`.`user_id` = `User`.`id`) WHERE 1 = 1

2 个答案:

答案 0 :(得分:2)

首先,使用$this->Post->exists()检查$user_id不会向您提供所需的例外情况。我想您要检查,如果User存在,请改用$this->Post->User->exists()

第二步 - 检查您的模型命名以及它们之间的关系(CategoryPostUser - 模型)您已经以某种方式得到Posts和{ {1}} SQL中的模型。我想,你已经添加了一个's',它不应该是;)

答案 1 :(得分:1)

您正在覆盖变量

这段代码:

$options = array('conditions' => array('Post.user_id' => $user_id));
$this->set('posts', $this->Post->find('all', $options));

负责查询2:

SELECT ... FROM `jaredisalie`.`posts` ... WHERE `Post`.`user_id` = 1      

这正是你所期待的。这是与问题中的代码相关的最后一个查询,但还有4个查询。

很明显/很明显,在查询5和6的同一请求中,帖子模型后面有后续发现:

SELECT ... FROM `jaredisalie`.`posts` AS `Post` ... WHERE 1 = 1 ORDER BY `Post`.`modified` desc LIMIT 20        
SELECT COUNT(*) AS `count` FROM `jaredisalie`.`posts` AS `Post` ... WHERE 1 = 1

就像你正在使用但不在问题中的代码一样,可能有这样的一行:

$this->set('posts', $this->paginate());

或类似 - 使用不同的变量名称,或者如果没有必要,只需删除它。

您有逻辑错误

此代码:

if (!$this->Post->exists($user_id)) {
    throw new NotFoundException(__('Invalid user'));
}

第一个查询:

SELECT COUNT(*) AS `count` FROM `jaredisalie`.`posts` AS `Post` WHERE `Post`.`id` = 1 

不合逻辑。该变量(据称)是用户ID - 但您正在检查是否存在具有该ID的帖子。