CakePHP 中的自定义分页查询错误
Custom pagination query error in CakePHP
我是 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()
)。但实际上不确定我的代码有什么问题。
网上找了很多地方都解决不了。我非常感谢任何建议。
好的,现在我明白我的代码有什么问题了。
问题不在控制器中。当我在 paginateCount()
函数中 return $results
时,我错了。 returning 结果的正确方法必须类似于 paginateCount()
函数中的 return $results[0][0]['count'];
。
因为paginateCount()
中的查询结果会return是这样的:
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'];
}
我是 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()
)。但实际上不确定我的代码有什么问题。
网上找了很多地方都解决不了。我非常感谢任何建议。
好的,现在我明白我的代码有什么问题了。
问题不在控制器中。当我在 paginateCount()
函数中 return $results
时,我错了。 returning 结果的正确方法必须类似于 paginateCount()
函数中的 return $results[0][0]['count'];
。
因为paginateCount()
中的查询结果会return是这样的:
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'];
}