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/1にアクセスすると、すべてのユーザーによるすべての投稿が返されます。http://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
4

2 に答える 2