Fix Controller::paginate and ordering with virtualFields.

Model::isVirtualField() does ensure that model names match the current model.
This creates false positives when doing sorting datasets when an associated
model has a field with the same name as a virtualField.
Fixes #1822. Fixes #1756.
This commit is contained in:
mark_story 2011-10-03 22:41:31 -04:00
parent 499b5e7268
commit a6e4208bdd
4 changed files with 34 additions and 3 deletions

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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
*

View file

@ -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()));