Permalink
Browse files

Fixed bug when using multi model fieldList and whitelists for all mod…

…els are not provided.

Fixes #3948
  • Loading branch information...
1 parent 19ac399 commit 473e4f991f18359c1b889005dae356efb1d7b44f @ADmad ADmad committed Aug 2, 2013
View
@@ -1621,9 +1621,10 @@ public function save($data = null, $validate = true, $fieldList = array()) {
}
if (!empty($options['fieldList'])) {
- $this->whitelist = $options['fieldList'];
if (!empty($options['fieldList'][$this->alias]) && is_array($options['fieldList'][$this->alias])) {
$this->whitelist = $options['fieldList'][$this->alias];
+ } elseif (Hash::dimensions($options['fieldList']) < 2) {
+ $this->whitelist = $options['fieldList'];
}
} elseif ($options['fieldList'] === null) {
$this->whitelist = array();
@@ -2338,7 +2339,10 @@ protected function _addToWhiteList($key, $options) {
$options['fieldList'][$this->alias][] = $key;
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;
}
return $options;
@@ -21,6 +21,7 @@
*/
App::uses('CakeValidationSet', 'Model/Validator');
+App::uses('Hash', 'Utility');
/**
* ModelValidator object encapsulates all methods related to data validations for a model
@@ -394,11 +395,11 @@ protected function _validationList($fieldList = array()) {
}
unset($fieldList);
- $validateList = array();
- if (empty($whitelist)) {
+ if (empty($whitelist) || Hash::dimensions($whitelist) > 1) {
return $this->_fields;
}
+ $validateList = array();
$this->validationErrors = array();
foreach ((array)$whitelist as $f) {
if (!empty($this->_fields[$f])) {
@@ -1522,7 +1522,7 @@ public function testSaveAllNotDeepValidateOnly() {
* @return void
*/
public function testValidateAssociated() {
- $this->loadFixtures('Comment', 'Attachment');
+ $this->loadFixtures('Comment', 'Attachment', 'Article', 'User');
$TestModel = new Comment();
$TestModel->Attachment->validate = array('attachment' => 'notEmpty');
@@ -1539,6 +1539,18 @@ public function testValidateAssociated() {
$result = $TestModel->validateAssociated($data);
$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');
$record = array(
'Comment' => array(
@@ -6536,7 +6536,7 @@ public function testSaveAllFieldListValidateBelongsTo() {
'order' => 'Post.id ASC',
));
$expected = array(
- 'Post' => array (
+ 'Post' => array(
'id' => '4',
'author_id' => '5',
'title' => 'Post without body',
@@ -6545,7 +6545,7 @@ public function testSaveAllFieldListValidateBelongsTo() {
'created' => self::date(),
'updated' => self::date(),
),
- 'Author' => array (
+ 'Author' => array(
'id' => '5',
'user' => 'bob',
'password' => null,
@@ -6559,6 +6559,66 @@ public function testSaveAllFieldListValidateBelongsTo() {
$this->assertEquals('', $result[3]['Post']['body']);
$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
$this->db->truncate($TestModel);

0 comments on commit 473e4f9

Please sign in to comment.