diff --git a/lib/Cake/Controller/Component/PaginatorComponent.php b/lib/Cake/Controller/Component/PaginatorComponent.php index c02f4f69e..3a80abaee 100644 --- a/lib/Cake/Controller/Component/PaginatorComponent.php +++ b/lib/Cake/Controller/Component/PaginatorComponent.php @@ -384,10 +384,11 @@ class PaginatorComponent extends Component { if (strpos($key, '.') !== false) { list($alias, $field) = explode('.', $key); } + $correctAlias = ($object->alias == $alias); - if ($object->hasField($field)) { + if ($correctAlias && $object->hasField($field)) { $order[$object->alias . '.' . $field] = $value; - } elseif ($object->hasField($key, true)) { + } elseif ($correctAlias && $object->hasField($key, true)) { $order[$field] = $value; } elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field, true)) { $order[$alias . '.' . $field] = $value; diff --git a/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php b/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php index 56959e9a2..d5dcbb51c 100644 --- a/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/PaginatorComponentTest.php @@ -959,6 +959,30 @@ class PaginatorComponentTest extends CakeTestCase { $this->assertEquals('desc', $result['order']['something']); } +/** + * test that sorting fields is alias specific + * + * @return void + */ + public function testValidateSortSharedFields() { + $model = $this->getMock('Model'); + $model->alias = 'Parent'; + $model->Child = $this->getMock('Model'); + $model->Child->alias = 'Child'; + + $model->expects($this->never()) + ->method('hasField'); + + $model->Child->expects($this->at(0)) + ->method('hasField') + ->with('something') + ->will($this->returnValue(true)); + + $options = array('sort' => 'Child.something', 'direction' => 'desc'); + $result = $this->Paginator->validateSort($model, $options); + + $this->assertEquals('desc', $result['order']['Child.something']); + } /** * test that multiple sort works. * @@ -1016,7 +1040,7 @@ class PaginatorComponentTest extends CakeTestCase { $options = array('sort' => 'Derp.id'); $result = $this->Paginator->validateSort($model, $options); - $this->assertEquals(array('Model.id' => 'asc'), $result['order']); + $this->assertEquals(array(), $result['order']); } /**