CakePHP排序顺序不影响所有记录

时间:2015-12-15 22:29:35

标签: mysql sorting cakephp cakephp-2.x cakephp-2.7

在我的CakePHP 2.7.7应用程序中,我有一个问题,即使用PaginatorComponent并不能正确排序我的结果。有关图像,请参阅此链接: Sample Data

数据应按姓氏降序排序,但您可以看到有几个用户似乎可以免除这些数据。无论我做什么顺序,升序或降序都没关系,这几条记录都没有排序。供参考:

UsersController中的index():

public function index() {
    $this->User->contain('Status');
    $this->paginate = array(
        'limit' => 15,
        'order' => array('User.last_name' => 'DESC'),
        'conditions' => array('User.department_id =' => $this->Session->read('Auth.User.department_id')),
        'contain' => 'Status'
    );
    $this->set('users', $this->Paginator->paginate());
}

任何可能导致这种情况的想法?我在这里不知所措。提前谢谢!

SELECT `User`.`id`, `User`.`first_name`, `User`.`last_name`, `User`.`middle`, `User`.`address`, `User`.`address_2`, `User`.`city`, `User`.`state_id`, `User`.`zip`, `User`.`home`, `User`.`cell`, `User`.`work`, `User`.`email`, `User`.`status_id`, `User`.`status_reason`, `User`.`rank`, `User`.`birthday`, `User`.`gender`, `User`.`school`, `User`.`employer`, `User`.`position`, `User`.`created`, `User`.`modified`, `User`.`updated_by`, `User`.`radio`, `User`.`ident`, `User`.`parent_name`, `User`.`parent_number`, `User`.`parent_email`, `User`.`squad`, `User`.`squad_leader`, `User`.`ride_along`, `User`.`drivers_license`, `User`.`username`, `User`.`password`, `User`.`department_id`, `User`.`join_date`, `User`.`group_id`, `User`.`test`, (CONCAT(`User`.`first_name`, " ", `User`.`last_name`)) AS `User__name`, `Status`.`id`, `Status`.`status` FROM `admin_cake`.`users` AS `User` LEFT JOIN `admin_cake`.`statuses` AS `Status` ON (`User`.`status_id` = `Status`.`id`) WHERE `User`.`department_id` = 1 ORDER BY `User`.`last_name` ASC LIMIT 15

1 个答案:

答案 0 :(得分:0)

当您使用$ this-> paginate设置条件时,限制...您需要使用函数$ this-> pagination()。 $ this->分页和$ this-> Paginator-> pagination()不能合并。

1)

public function index() {
    $this->User->contain('Status');
    $this->paginate = array(
        'limit' => 15,
        'order' => array('User.last_name' => 'DESC'),
        'conditions' => array('User.department_id =' => $this->Session->read('Auth.User.department_id')),
        'contain' => 'Status'
    );
    // Assign settings
    $this->Paginator->settings = $this->paginate;
    $this->set('users', $this->Paginator->paginate());
}

2)

public function index() {
    $this->User->contain('Status');
    $this->paginate = array(
        'limit' => 15,
        'order' => array('User.last_name' => 'DESC'),
        'conditions' => array('User.department_id =' => $this->Session->read('Auth.User.department_id')),
        'contain' => 'Status'
    );
    //Don't use $this->Paginator->paginate()
    $this->set('users', $this->paginate());
}