【问题标题】:Returning values with a foreign key CakePHP使用外键返回值 CakePHP
【发布时间】:2013-06-20 16:55:16
【问题描述】:

我目前正在将 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

【问题讨论】:

  • 显示生成的 SQL 查询。

标签: cakephp cakephp-2.0


【解决方案1】:

首先,检查$this->Post->exists()$user_id 不会给你想要的例外。我想你想检查一下,如果User 存在,请改用$this->Post->User->exists()

第二 - 检查您的模型命名和它们之间的关系(在CategoryPostUser - 模型中)您的 SQL 中不知何故有一个 Posts 和一个 Post 模型。我猜,你在不应该的地方添加了一个 's' ;)

【讨论】:

  • +1 表示最后一点。从查询来看,类别模型可能定义为 Category hasMany PostS。
  • 感谢您对复数模型错误和我普遍缺乏 cakephp 知识的看法是正确的:D
【解决方案2】:

你正在覆盖你的变量

这段代码:

$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 的同一请求中,在此之后的 post 模型上有后续发现是显而易见的:

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 的帖子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    相关资源
    最近更新 更多