Cakephp显示1 = 1

时间:2015-05-21 05:23:41

标签: php mysql cakephp cakephp-2.5

您好我在这里创建简单的博客网站在登录页面查看博客但是如果用户选择任何类别,则此类别下的博客仅列出。我的问题是,当用户从右侧边栏选择类别时,它返回所有表记录。 当我打印查询时,它返回:

array(
    'log' => array(
        (int) 0 => array(
            'query' => 'SELECT `Category`.`category_id`, `Category`.`name` FROM `cakeBlog`.`categories` AS `Category`   WHERE 1 = 1',
            'params' => array(),
            'affected' => (int) 13,
            'numRows' => (int) 13,
            'took' => (float) 0
        ),
        (int) 1 => array(
            'query' => 'SELECT `Post`.`id`, `Post`.`category_id`, `Post`.`title`, `Post`.`body`, `Post`.`created`, `Post`.`modified`, `Post`.`user_id` FROM `cakeBlog`.`posts` AS `Post`   WHERE 1 = 1    LIMIT 20',
            'params' => array(),
            'affected' => (int) 13,
            'numRows' => (int) 13,
            'took' => (float) 0
        )
    ),
    'count' => (int) 2,
    'time' => (float) 0
)

这是我的控制器文件 PostsController.php

在索引功能中,我会检查类别ID是否存在?

请帮帮我... 感谢

1 个答案:

答案 0 :(得分:0)

使用以下要获取类别的块

if(is_null($categoryId)) {
    $this->Paginator->setting = array(
        'limit' =>'10',
        'order' => array('Post.id' =>'Desc')
        /*'conditions' => array(
            'Post.user_id' => AuthComponent::user(id)
        )*/
    );
    $arrPosts = $this->Paginator->paginate('Post');
    $this->set('posts', $arrPosts);
} else {
    $condition = array('Post.category_id' => $categoryId,'limit' =>'10');
    $da = $this->Paginator->setting = array(
        'conditions' => $condition      
        /*'conditions' => array(
        'Post.user_id' => AuthComponent::user(id)
        )*/
    );

    $arrPosts = $this->Paginator->paginate('Post');
    $log = $this->Post->getDataSource()->getLog(false, false); debug($log);
    $this->set('posts', $arrPosts);
}