diff --git a/lib/Cake/Controller/Component/PaginatorComponent.php b/lib/Cake/Controller/Component/PaginatorComponent.php index 25c0cb471..bd3536289 100644 --- a/lib/Cake/Controller/Component/PaginatorComponent.php +++ b/lib/Cake/Controller/Component/PaginatorComponent.php @@ -340,22 +340,23 @@ class PaginatorComponent extends Component { } if (!empty($options['order']) && is_array($options['order'])) { - $alias = $object->alias; - $key = $field = key($options['order']); + $order = array(); + foreach ($options['order'] as $key => $value) { + $field = $key; + $alias = $object->alias; + if (strpos($key, '.') !== false) { + list($alias, $field) = explode('.', $key); + } - if (strpos($key, '.') !== false) { - list($alias, $field) = explode('.', $key); - } - $value = $options['order'][$key]; - unset($options['order'][$key]); - - if ($object->hasField($field)) { - $options['order'][$alias . '.' . $field] = $value; - } elseif ($object->hasField($key, true)) { - $options['order'][$field] = $value; - } elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field)) { - $options['order'][$alias . '.' . $field] = $value; + if ($object->hasField($field)) { + $order[$alias . '.' . $field] = $value; + } elseif ($object->hasField($key, true)) { + $order[$field] = $value; + } elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field)) { + $order[$alias . '.' . $field] = $value; + } } + $options['order'] = $order; } return $options; diff --git a/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php b/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php index 542c659e6..1a754fdd7 100644 --- a/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php @@ -683,6 +683,29 @@ class PaginatorComponentTest extends CakeTestCase { $this->assertEquals('desc', $result['order']['something']); } +/** + * test that multiple sort works. + * + * @return void + */ + public function testValidateSortMultiple() { + $model = $this->getMock('Model'); + $model->alias = 'model'; + $model->expects($this->any())->method('hasField')->will($this->returnValue(true)); + + $options = array('order' => array( + 'author_id' => 'asc', + 'title' => 'asc' + )); + $result = $this->Paginator->validateSort($model, $options); + $expected = array( + 'model.author_id' => 'asc', + 'model.title' => 'asc' + ); + + $this->assertEquals($expected, $result['order']); + } + /** * test that maxLimit is respected *