Skip to content

Commit

Permalink
Merge pull request #1398 from dereuromark/master-paginator
Browse files Browse the repository at this point in the history
PaginatorComponent should use model->order if there are no other order conditions.

Fixes #3902
  • Loading branch information
markstory committed Jul 4, 2013
2 parents 7267383 + a620fbb commit 7607087
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/Cake/Controller/Component/PaginatorComponent.php
Expand Up @@ -396,7 +396,9 @@ public function validateSort(Model $object, array $options, array $whitelist = a
}
$options['order'] = $order;
}

if (empty($options['order']) && !empty($object->order)) {
$options['order'] = $object->order;
}
return $options;
}

Expand Down
25 changes: 25 additions & 0 deletions lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php
Expand Up @@ -577,6 +577,31 @@ public function testDefaultPaginateParams() {
$this->assertEquals(array(3, 2, 1), $results);
}

/**
* test paginate() and model default order
*
* @return void
*/
public function testPaginateOrderModelDefault() {
$Controller = new PaginatorTestController($this->request);
$Controller->uses = array('PaginatorControllerPost');
$Controller->params['url'] = array();
$Controller->constructClasses();
$Controller->PaginatorControllerPost->order = array(
$Controller->PaginatorControllerPost->alias . '.created' => 'desc'
);

$Controller->Paginator->settings = array(
'fields' => array('id', 'title', 'created'),
'maxLimit' => 10,
'paramType' => 'named'
);
$result = $Controller->Paginator->paginate('PaginatorControllerPost');
$expected = array('2007-03-18 10:43:23', '2007-03-18 10:41:23', '2007-03-18 10:39:23');
$this->assertEquals($expected, Hash::extract($result, '{n}.PaginatorControllerPost.created'));
$this->assertEquals($Controller->PaginatorControllerPost->order, $this->Controller->request['paging']['PaginatorControllerPost']['order']);
}

/**
* test paginate() and virtualField interactions
*
Expand Down

0 comments on commit 7607087

Please sign in to comment.