diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index 371fc74cf..20fb8dfdd 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -1126,7 +1126,7 @@ class Controller extends Object { } if (!empty($options['order']) && is_array($options['order'])) { - $alias = $object->alias ; + $alias = $object->alias; $key = $field = key($options['order']); if (strpos($key, '.') !== false) { @@ -1137,7 +1137,7 @@ class Controller extends Object { if ($object->hasField($field)) { $options['order'][$alias . '.' . $field] = $value; - } elseif ($object->hasField($field, true)) { + } elseif ($object->hasField($key, true)) { $options['order'][$field] = $value; } elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field)) { $options['order'][$alias . '.' . $field] = $value; diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 7d29ed461..764124191 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1037,7 +1037,7 @@ class Model extends Overloadable { } if (strpos($field, '.') !== false) { list($model, $field) = explode('.', $field); - if (isset($this->virtualFields[$field])) { + if ($model == $this->alias && isset($this->virtualFields[$field])) { return true; } } diff --git a/cake/tests/cases/libs/controller/controller.test.php b/cake/tests/cases/libs/controller/controller.test.php index e7adb46ad..729b9a0ab 100644 --- a/cake/tests/cases/libs/controller/controller.test.php +++ b/cake/tests/cases/libs/controller/controller.test.php @@ -832,6 +832,36 @@ class ControllerTest extends CakeTestCase { $this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(2, 3, 4)); } +/** + * test paginate() and virtualField overlapping with real fields. + * + * @return void + */ + function testPaginateOrderVirtualFieldSharedWithRealField() { + $Controller =& new Controller(); + $Controller->uses = array('ControllerPost', 'ControllerComment'); + $Controller->params['url'] = array(); + $Controller->constructClasses(); + $Controller->ControllerComment->virtualFields = array( + 'title' => 'ControllerComment.comment' + ); + $Controller->ControllerComment->bindModel(array( + 'belongsTo' => array( + 'ControllerPost' => array( + 'className' => 'ControllerPost', + 'foreignKey' => 'article_id' + ) + ) + ), false); + + $Controller->paginate = array( + 'fields' => array('ControllerComment.id', 'title', 'ControllerPost.title'), + ); + $Controller->passedArgs = array('sort' => 'ControllerPost.title', 'dir' => 'asc'); + $result = $Controller->paginate('ControllerComment'); + $this->assertEqual(Set::extract($result, '{n}.ControllerComment.id'), array(1, 2, 3, 4, 5, 6)); + } + /** * testFlash method * diff --git a/cake/tests/cases/libs/model/model_read.test.php b/cake/tests/cases/libs/model/model_read.test.php index c4e89fd04..c553be802 100755 --- a/cake/tests/cases/libs/model/model_read.test.php +++ b/cake/tests/cases/libs/model/model_read.test.php @@ -7435,6 +7435,7 @@ class ModelReadTest extends BaseModelTest { $this->assertTrue($Post->isVirtualField('other_field')); $this->assertTrue($Post->isVirtualField('Post.other_field')); + $this->assertFalse($Post->isVirtualField('Comment.other_field'), 'Other models should not match.'); $this->assertFalse($Post->isVirtualField('id')); $this->assertFalse($Post->isVirtualField('Post.id')); $this->assertFalse($Post->isVirtualField(array()));