diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index 6362166a3..bed3419c1 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -1621,9 +1621,10 @@ class Model extends Object implements CakeEventListener { } 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 @@ class Model extends Object implements CakeEventListener { $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; diff --git a/lib/Cake/Model/ModelValidator.php b/lib/Cake/Model/ModelValidator.php index cddf27b5d..f85133881 100644 --- a/lib/Cake/Model/ModelValidator.php +++ b/lib/Cake/Model/ModelValidator.php @@ -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 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable { } 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])) { diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index 5dd9fe2c9..031a9436f 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -1522,7 +1522,7 @@ class ModelValidationTest extends BaseModelTest { * @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 @@ class ModelValidationTest extends BaseModelTest { $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( diff --git a/lib/Cake/Test/Case/Model/ModelWriteTest.php b/lib/Cake/Test/Case/Model/ModelWriteTest.php index fbd63b475..b4864e0c2 100644 --- a/lib/Cake/Test/Case/Model/ModelWriteTest.php +++ b/lib/Cake/Test/Case/Model/ModelWriteTest.php @@ -6536,7 +6536,7 @@ class ModelWriteTest extends BaseModelTest { 'order' => 'Post.id ASC', )); $expected = array( - 'Post' => array ( + 'Post' => array( 'id' => '4', 'author_id' => '5', 'title' => 'Post without body', @@ -6545,7 +6545,7 @@ class ModelWriteTest extends BaseModelTest { 'created' => self::date(), 'updated' => self::date(), ), - 'Author' => array ( + 'Author' => array( 'id' => '5', 'user' => 'bob', 'password' => null, @@ -6559,6 +6559,66 @@ class ModelWriteTest extends BaseModelTest { $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);