mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-03-21 08:59:57 +00:00
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:
parent
499b5e7268
commit
a6e4208bdd
4 changed files with 34 additions and 3 deletions
cake
libs
tests/cases/libs
|
@ -1126,7 +1126,7 @@ class Controller extends Object {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($options['order']) && is_array($options['order'])) {
|
if (!empty($options['order']) && is_array($options['order'])) {
|
||||||
$alias = $object->alias ;
|
$alias = $object->alias;
|
||||||
$key = $field = key($options['order']);
|
$key = $field = key($options['order']);
|
||||||
|
|
||||||
if (strpos($key, '.') !== false) {
|
if (strpos($key, '.') !== false) {
|
||||||
|
@ -1137,7 +1137,7 @@ class Controller extends Object {
|
||||||
|
|
||||||
if ($object->hasField($field)) {
|
if ($object->hasField($field)) {
|
||||||
$options['order'][$alias . '.' . $field] = $value;
|
$options['order'][$alias . '.' . $field] = $value;
|
||||||
} elseif ($object->hasField($field, true)) {
|
} elseif ($object->hasField($key, true)) {
|
||||||
$options['order'][$field] = $value;
|
$options['order'][$field] = $value;
|
||||||
} elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field)) {
|
} elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field)) {
|
||||||
$options['order'][$alias . '.' . $field] = $value;
|
$options['order'][$alias . '.' . $field] = $value;
|
||||||
|
|
|
@ -1037,7 +1037,7 @@ class Model extends Overloadable {
|
||||||
}
|
}
|
||||||
if (strpos($field, '.') !== false) {
|
if (strpos($field, '.') !== false) {
|
||||||
list($model, $field) = explode('.', $field);
|
list($model, $field) = explode('.', $field);
|
||||||
if (isset($this->virtualFields[$field])) {
|
if ($model == $this->alias && isset($this->virtualFields[$field])) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -832,6 +832,36 @@ class ControllerTest extends CakeTestCase {
|
||||||
$this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(2, 3, 4));
|
$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
|
* testFlash method
|
||||||
*
|
*
|
||||||
|
|
|
@ -7435,6 +7435,7 @@ class ModelReadTest extends BaseModelTest {
|
||||||
|
|
||||||
$this->assertTrue($Post->isVirtualField('other_field'));
|
$this->assertTrue($Post->isVirtualField('other_field'));
|
||||||
$this->assertTrue($Post->isVirtualField('Post.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('id'));
|
||||||
$this->assertFalse($Post->isVirtualField('Post.id'));
|
$this->assertFalse($Post->isVirtualField('Post.id'));
|
||||||
$this->assertFalse($Post->isVirtualField(array()));
|
$this->assertFalse($Post->isVirtualField(array()));
|
||||||
|
|
Loading…
Add table
Reference in a new issue