mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-02-07 12:36:25 +00:00
Fixed bug when using multi model fieldList and whitelists for all models are not provided.
Fixes #3948
This commit is contained in:
parent
19ac39963a
commit
473e4f991f
4 changed files with 84 additions and 7 deletions
|
@ -1621,9 +1621,10 @@ class Model extends Object implements CakeEventListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($options['fieldList'])) {
|
if (!empty($options['fieldList'])) {
|
||||||
$this->whitelist = $options['fieldList'];
|
|
||||||
if (!empty($options['fieldList'][$this->alias]) && is_array($options['fieldList'][$this->alias])) {
|
if (!empty($options['fieldList'][$this->alias]) && is_array($options['fieldList'][$this->alias])) {
|
||||||
$this->whitelist = $options['fieldList'][$this->alias];
|
$this->whitelist = $options['fieldList'][$this->alias];
|
||||||
|
} elseif (Hash::dimensions($options['fieldList']) < 2) {
|
||||||
|
$this->whitelist = $options['fieldList'];
|
||||||
}
|
}
|
||||||
} elseif ($options['fieldList'] === null) {
|
} elseif ($options['fieldList'] === null) {
|
||||||
$this->whitelist = array();
|
$this->whitelist = array();
|
||||||
|
@ -2338,7 +2339,10 @@ class Model extends Object implements CakeEventListener {
|
||||||
$options['fieldList'][$this->alias][] = $key;
|
$options['fieldList'][$this->alias][] = $key;
|
||||||
return $options;
|
return $options;
|
||||||
}
|
}
|
||||||
if (!empty($options['fieldList']) && is_array($options['fieldList'])) {
|
if (!empty($options['fieldList']) &&
|
||||||
|
is_array($options['fieldList']) &&
|
||||||
|
Hash::dimensions($options['fieldList']) < 2
|
||||||
|
) {
|
||||||
$options['fieldList'][] = $key;
|
$options['fieldList'][] = $key;
|
||||||
}
|
}
|
||||||
return $options;
|
return $options;
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
App::uses('CakeValidationSet', 'Model/Validator');
|
App::uses('CakeValidationSet', 'Model/Validator');
|
||||||
|
App::uses('Hash', 'Utility');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ModelValidator object encapsulates all methods related to data validations for a model
|
* ModelValidator object encapsulates all methods related to data validations for a model
|
||||||
|
@ -394,11 +395,11 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
|
||||||
}
|
}
|
||||||
unset($fieldList);
|
unset($fieldList);
|
||||||
|
|
||||||
$validateList = array();
|
if (empty($whitelist) || Hash::dimensions($whitelist) > 1) {
|
||||||
if (empty($whitelist)) {
|
|
||||||
return $this->_fields;
|
return $this->_fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$validateList = array();
|
||||||
$this->validationErrors = array();
|
$this->validationErrors = array();
|
||||||
foreach ((array)$whitelist as $f) {
|
foreach ((array)$whitelist as $f) {
|
||||||
if (!empty($this->_fields[$f])) {
|
if (!empty($this->_fields[$f])) {
|
||||||
|
|
|
@ -1522,7 +1522,7 @@ class ModelValidationTest extends BaseModelTest {
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function testValidateAssociated() {
|
public function testValidateAssociated() {
|
||||||
$this->loadFixtures('Comment', 'Attachment');
|
$this->loadFixtures('Comment', 'Attachment', 'Article', 'User');
|
||||||
$TestModel = new Comment();
|
$TestModel = new Comment();
|
||||||
$TestModel->Attachment->validate = array('attachment' => 'notEmpty');
|
$TestModel->Attachment->validate = array('attachment' => 'notEmpty');
|
||||||
|
|
||||||
|
@ -1539,6 +1539,18 @@ class ModelValidationTest extends BaseModelTest {
|
||||||
$result = $TestModel->validateAssociated($data);
|
$result = $TestModel->validateAssociated($data);
|
||||||
$this->assertFalse($result);
|
$this->assertFalse($result);
|
||||||
|
|
||||||
|
$fieldList = array(
|
||||||
|
'Attachment' => array('comment_id')
|
||||||
|
);
|
||||||
|
$result = $TestModel->saveAll($data, array(
|
||||||
|
'fieldList' => $fieldList, 'validate' => 'only'
|
||||||
|
));
|
||||||
|
$this->assertTrue($result);
|
||||||
|
$this->assertEmpty($TestModel->validationErrors);
|
||||||
|
$result = $TestModel->validateAssociated($data, array('fieldList' => $fieldList));
|
||||||
|
$this->assertTrue($result);
|
||||||
|
$this->assertEmpty($TestModel->validationErrors);
|
||||||
|
|
||||||
$TestModel->validate = array('comment' => 'notEmpty');
|
$TestModel->validate = array('comment' => 'notEmpty');
|
||||||
$record = array(
|
$record = array(
|
||||||
'Comment' => array(
|
'Comment' => array(
|
||||||
|
|
|
@ -6536,7 +6536,7 @@ class ModelWriteTest extends BaseModelTest {
|
||||||
'order' => 'Post.id ASC',
|
'order' => 'Post.id ASC',
|
||||||
));
|
));
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'Post' => array (
|
'Post' => array(
|
||||||
'id' => '4',
|
'id' => '4',
|
||||||
'author_id' => '5',
|
'author_id' => '5',
|
||||||
'title' => 'Post without body',
|
'title' => 'Post without body',
|
||||||
|
@ -6545,7 +6545,7 @@ class ModelWriteTest extends BaseModelTest {
|
||||||
'created' => self::date(),
|
'created' => self::date(),
|
||||||
'updated' => self::date(),
|
'updated' => self::date(),
|
||||||
),
|
),
|
||||||
'Author' => array (
|
'Author' => array(
|
||||||
'id' => '5',
|
'id' => '5',
|
||||||
'user' => 'bob',
|
'user' => 'bob',
|
||||||
'password' => null,
|
'password' => null,
|
||||||
|
@ -6559,6 +6559,66 @@ class ModelWriteTest extends BaseModelTest {
|
||||||
$this->assertEquals('', $result[3]['Post']['body']);
|
$this->assertEquals('', $result[3]['Post']['body']);
|
||||||
$this->assertEquals('working', $result[3]['Author']['test']);
|
$this->assertEquals('working', $result[3]['Author']['test']);
|
||||||
|
|
||||||
|
$fieldList = array(
|
||||||
|
'Post' => array('title')
|
||||||
|
);
|
||||||
|
$data = array(
|
||||||
|
'Post' => array(
|
||||||
|
'title' => 'Post without body 2',
|
||||||
|
'body' => 'This will not be saved'
|
||||||
|
),
|
||||||
|
'Author' => array(
|
||||||
|
'user' => 'jack'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$TestModel->saveAll($data, array('fieldList' => $fieldList));
|
||||||
|
$result = $TestModel->find('all', array(
|
||||||
|
'order' => 'Post.id ASC',
|
||||||
|
));
|
||||||
|
$this->assertNull($result[4]['Post']['body']);
|
||||||
|
|
||||||
|
$fieldList = array(
|
||||||
|
'Author' => array('password')
|
||||||
|
);
|
||||||
|
$data = array(
|
||||||
|
'Post' => array(
|
||||||
|
'id' => '5',
|
||||||
|
'title' => 'Post title',
|
||||||
|
'body' => 'Post body'
|
||||||
|
),
|
||||||
|
'Author' => array(
|
||||||
|
'id' => '6',
|
||||||
|
'user' => 'will not change',
|
||||||
|
'password' => 'foobar'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$result = $TestModel->saveAll($data, array('fieldList' => $fieldList));
|
||||||
|
$this->assertTrue($result);
|
||||||
|
|
||||||
|
$result = $TestModel->find('all', array(
|
||||||
|
'order' => 'Post.id ASC',
|
||||||
|
));
|
||||||
|
$expected = array(
|
||||||
|
'Post' => array(
|
||||||
|
'id' => '5',
|
||||||
|
'author_id' => '6',
|
||||||
|
'title' => 'Post title',
|
||||||
|
'body' => 'Post body',
|
||||||
|
'published' => 'N',
|
||||||
|
'created' => self::date(),
|
||||||
|
'updated' => self::date()
|
||||||
|
),
|
||||||
|
'Author' => array(
|
||||||
|
'id' => '6',
|
||||||
|
'user' => 'jack',
|
||||||
|
'password' => 'foobar',
|
||||||
|
'created' => self::date(),
|
||||||
|
'updated' => self::date(),
|
||||||
|
'test' => 'working'
|
||||||
|
),
|
||||||
|
);
|
||||||
|
$this->assertEquals($expected, $result[4]);
|
||||||
|
|
||||||
// test multirecord
|
// test multirecord
|
||||||
$this->db->truncate($TestModel);
|
$this->db->truncate($TestModel);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue