Skip to content
Permalink
Browse files

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.
  • Loading branch information...
markstory committed Oct 4, 2011
1 parent 499b5e7 commit a6e4208bdd4fa500687d07a76a7d06c529e935fb
@@ -1126,7 +1126,7 @@ function paginate($object = null, $scope = array(), $whitelist = array()) {
}
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 @@ function paginate($object = null, $scope = array(), $whitelist = array()) {
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;
@@ -1037,7 +1037,7 @@ function isVirtualField($field) {
}
if (strpos($field, '.') !== false) {
list($model, $field) = explode('.', $field);
if (isset($this->virtualFields[$field])) {
if ($model == $this->alias && isset($this->virtualFields[$field])) {
return true;
}
}
@@ -832,6 +832,36 @@ function testPaginateOrderVirtualField() {
$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
*
@@ -7435,6 +7435,7 @@ function testIsVirtualField() {
$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()));

0 comments on commit a6e4208

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.