如何在cakephp中对自定义查询进行分页

时间:2014-11-28 12:06:09

标签: php sql cakephp

$l = $this->Manager->query("SELECT leave_application.id
         from leave_application INNER JOIN emp_managers ON (leave_application.employee_id = emp_managers.employee_id) 
         WHERE emp_managers.manager_id =" . $this->Session->read('Auth.Manager.id')
         ." AND leave_application.approve_1 !=". $id ." AND leave_application.approve_2 != ". $id 
         ." AND leave_application.approve_3 != ". $id);

修改
这是我尝试过的,但确实有效:

function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra)
{
    return $this->query("SELECT leave_application.id as leaveID, leave_application.approve_1,leave_application.employee_id,
        leave_application.approve_2, leave_application.days_desired ,leave_application.approve_3,leave_application.leave_from, 
        leave_application.leave_to, employees.id, employees.firstname , employees.lastname, emp_managers.employee_id as empmanagers_id 
        from leave_application INNER JOIN emp_managers ON (leave_application.employee_id = emp_managers.employee_id) 
        INNER JOIN employees on (employees.id = emp_managers.employee_id)
        WHERE emp_managers.manager_id =" . CakeSession::read('Auth.Manager.id')
    ." and leave_application.approve_1 !=". CakeSession::read('Auth.Manager.id')
    ." and leave_application.approve_2 != ". CakeSession::read('Auth.Manager.id')
    ." and leave_application.approve_3 != ". CakeSession::read('Auth.Manager.id'));
}

function paginateCount($conditions, $recursive, $extra)
{
    return $this->query("SELECT COUNT(*) as count  
        from leave_application INNER JOIN emp_managers ON (leave_application.employee_id = emp_managers.employee_id) 
        INNER JOIN employees on (employees.id = emp_managers.employee_id)
        WHERE emp_managers.manager_id =" . CakeSession::read('Auth.Manager.id')
    ." and leave_application.approve_1 !=". CakeSession::read('Auth.Manager.id')
    ." and leave_application.approve_2 != ". CakeSession::read('Auth.Manager.id') 
    ." and leave_application.approve_3 != ". CakeSession::read('Auth.Manager.id'));
}

1 个答案:

答案 0 :(得分:0)

使用相同的查询将这些方法添加到控制器模型中,而不使用限制关键字

function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra)
{
    return $this->query('SELECT ...');
}

function paginateCount($conditions, $recursive, $extra)
{
    return $this->query('SELECT COUNT(.....');
}