CakePHP中的自定义分页查询错误

时间:2017-04-27 16:34:18

标签: pagination cakephp-2.8

我是CakePHP的新手。现在,我正在使用cakephp(版本2.8.5)。

我尝试创建自定义查询分页,因为我需要连接多个表。但是,它不起作用,我得到Unsupported operand types错误。

在我的User模型中,我尝试创建paginate()paginateCount()功能,如下所示。

    public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {

            $recursive = -1;

            // // Mandatory to have
            // $this->useTable = false;
            $sql = '';

            $sql .= "SELECT u.id,u.name, u.password, r.display_name, GROUP_CONCAT(p.display_name SEPARATOR ', ') as per_name ";
            $sql .= "FROM `users` u ";
            $sql .= "LEFT JOIN roles r ON (u.role_id = r.id) ";
            $sql .= "LEFT JOIN user_permission up ON (u.id = up.user_id) ";
            $sql .= "LEFT JOIN permissions p ON (up.permission = p.id) ";
            $sql .= "WHERE 1 GROUP BY u.name ORDER BY u.id ";
            // Adding LIMIT Clause
            $sql .= "LIMIT ".(($page - 1) * $limit) . ', ' . $limit;

            $results = $this->query($sql);

            return $results;
        }

        public function paginateCount($conditions = null, $recursive = 0,
            $extra = array()) {

            $sql = '';

            $sql .= "SELECT COUNT(*) as count FROM
                (SELECT u.id,u.name, u.password, r.display_name, GROUP_CONCAT(p.display_name SEPARATOR ', ') as per_name
                FROM `users` u 
                LEFT JOIN roles r ON (u.role_id = r.id) LEFT JOIN user_permission up ON (u.id = up.user_id) 
                LEFT JOIN permissions p ON (up.permission = p.id) 
                WHERE 1 
                GROUP BY u.name
                ORDER BY u.id ) AS Temp";

            $this->recursive = $recursive;

            $results = $this->query($sql);

            return $results;
        }

在我的UsersController

    public function index() {

        $users = $this->User->getAllUser();

        //for pagination
        $this->paginate = array(
            'limit' => 4
        );
        $page = $this->paginate();
        $count = $this->paginateCount();

        $this->set('page',$page);
        $this->set('rowCount',$count);
        $this->set('users',$users);
        $this->render('index');

    }

但它不起作用,我得到Unsupported operand types致命错误。我想在这里可能是我错了($this->paginate())。但实际上我不确定我的代码有什么问题。

我已经在网上搜索了很多地方但无法解决。对于任何建议,我都非常感激。

1 个答案:

答案 0 :(得分:0)

好的,现在我明白我的代码中出了什么问题。

问题不在控制器中。当我在$results函数中返回paginateCount()时,我错了。返回结果的正确方法必须与return $results[0][0]['count'];函数中的paginateCount()类似。

因为paginateCount()中的查询结果将返回如下:

  

array(1){[0] => array(1){[0] => array(1){[“count”] => string(1)“5”   }}

paginateCount()的完整代码如下所示。

    public function paginateCount($conditions = null, $recursive = 0,
            $extra = array()) {

            $sql = '';

            $sql = "SELECT COUNT(*) as count FROM
                (SELECT User.id,User.name, User.password, r.display_name, GROUP_CONCAT(p.display_name SEPARATOR ', ') as per_name
                FROM `users` User 
                LEFT JOIN roles r ON (User.role_id = r.id) LEFT JOIN user_permission up ON (User.id = up.user_id) 
                LEFT JOIN permissions p ON (up.permission = p.id) 
                WHERE 1 
                GROUP BY User.name
                ORDER BY User.id ) AS Temp";

            $this->recursive = $recursive;

            $results = $this->query($sql);
            return $results[0][0]['count'];
    }