Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

ticket-3902 - paginator and display of order via model default order

  • Loading branch information...
commit f680c763b248d65303cc8ffa4ec4733ce1670682 1 parent 7267383
Mark S. dereuromark authored
4 lib/Cake/Controller/Component/PaginatorComponent.php
View
@@ -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;
}
25 lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php
View
@@ -578,6 +578,31 @@ public function testDefaultPaginateParams() {
}
/**
+ * 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
*
* @return void

1 comment on commit f680c76

ADmad
Collaborator

This change causes problems when Model::$order contains multiple fields as the paginator helper can't handle multi field ordering.

If I have $order = ['Foo.field_1', 'Foo.field_2']; it generates paging links with query string like ?page=2&sort=0&direction=Foo.field_1. Also its possible to set Model::$order to a string which would also generate weird paging links.

Please sign in to comment.
Something went wrong with that request. Please try again.