Permalink
Browse files

Paginator can now use string or simple arrays for "order

"order" => "Posts.id" wasn't use by the paginator
It was expecting [key => order, key => order]
  • Loading branch information...
1 parent b6b3287 commit aec95640a0b83b924ebe26c9b21d89bcf5e284a7 @Grafikart Grafikart committed Jan 29, 2014
@@ -293,7 +293,7 @@ public function validateSort(Table $object, array $options) {
$options['order'] = [];
}
if (!is_array($options['order'])) {
- $options['order'] = (array)$options['order'];
+ return $options;
}
if (!empty($options['sortWhitelist'])) {
@@ -312,13 +312,17 @@ public function validateSort(Table $object, array $options) {
foreach ($options['order'] as $key => $value) {
$field = $key;
$alias = $tableAlias;
- if (strpos($key, '.') !== false) {
- list($alias, $field) = explode('.', $key);
- }
- $correctAlias = ($tableAlias == $alias);
+ if (is_numeric($key)){
+ $order[] = $value;
+ }else{
+ if (strpos($key, '.') !== false) {
+ list($alias, $field) = explode('.', $key);
+ }
+ $correctAlias = ($tableAlias == $alias);
- if ($correctAlias && $object->hasField($field)) {
- $order[$tableAlias . '.' . $field] = $value;
+ if ($correctAlias && $object->hasField($field)) {
+ $order[$tableAlias . '.' . $field] = $value;
+ }
}
}
$options['order'] = $order;
@@ -487,6 +487,48 @@ public function testValidateSortMultiple() {
$this->assertEquals($expected, $result['order']);
}
+/**
+ * test that string order are used by paginator
+ *
+ * @return void
+ */
+ public function testValidateSortWithString() {
+ $model = $this->getMock('Cake\ORM\Table');
+ $model->expects($this->any())
+ ->method('alias')
+ ->will($this->returnValue('model'));
+ $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
+
+ $options = array(
+ 'order' => 'model.author_id DESC'
+ );
+ $result = $this->Paginator->validateSort($model, $options);
+ $expected = 'model.author_id DESC';
+
+ $this->assertEquals($expected, $result['order']);
+ }
+
+/**
+ * test that numeric array order are used by paginator
+ *
+ * @return void
+ */
+ public function testValidateSortWithNumericArray() {
+ $model = $this->getMock('Cake\ORM\Table');
+ $model->expects($this->any())
+ ->method('alias')
+ ->will($this->returnValue('model'));
+ $model->expects($this->any())->method('hasField')->will($this->returnValue(true));
+
+ $options = array(
+ 'order' => array('model.author_id DESC')
+ );
+ $result = $this->Paginator->validateSort($model, $options);
+ $expected = array('model.author_id DESC');
+
+ $this->assertEquals($expected, $result['order']);
+ }
+
/**
* Test that no sort doesn't trigger an error.
*

0 comments on commit aec9564

Please sign in to comment.