Skip to content

Commit

Permalink
Changed default value for option 'validate' to 'first' in Model::save…
Browse files Browse the repository at this point in the history
…All(). Also fixed issue where the return array contained more keys then number of records in data array itself with options 'validate =>'first' and 'atomic'=>false
  • Loading branch information
ADmad committed Mar 27, 2010
1 parent 7b28fde commit d365faf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 6 additions & 4 deletions cake/libs/model/model.php
Expand Up @@ -1556,9 +1556,9 @@ function _prepareUpdateFields($data) {
*
* #### Options
*
* - validate: Set to false to disable validation, true to validate each record before
* saving, 'first' to validate *all* records before any are saved, or 'only' to only
* validate the records, but not save them.
* - validate: Set to false to disable validation, true to validate each record before saving,
* 'first' to validate *all* records before any are saved (default),
* or 'only' to only validate the records, but not save them.
* - atomic: If true (default), will attempt to save all records in a single transaction.
* Should be set to false if database/table does not support transactions.
* - fieldList: Equivalent to the $fieldList parameter in Model::save()
Expand All @@ -1579,7 +1579,7 @@ function saveAll($data = null, $options = array()) {
}
$db =& ConnectionManager::getDataSource($this->useDbConfig);

$options = array_merge(array('validate' => true, 'atomic' => true), $options);
$options = array_merge(array('validate' => 'first', 'atomic' => true), $options);
$this->validationErrors = $validationErrors = array();
$validates = true;
$return = array();
Expand Down Expand Up @@ -1626,6 +1626,7 @@ function saveAll($data = null, $options = array()) {
break;
case ($options['validate'] === 'first'):
$options['validate'] = true;
$return = array();
continue;
break;
default:
Expand Down Expand Up @@ -1731,6 +1732,7 @@ function saveAll($data = null, $options = array()) {
break;
case ($options['validate'] === 'first'):
$options['validate'] = true;
$return = array();
continue;
break;
default:
Expand Down
9 changes: 5 additions & 4 deletions cake/tests/cases/libs/model/model_write.test.php
Expand Up @@ -2871,7 +2871,8 @@ function testSaveAllAtomic() {
'title' => '',
'body' => 'Trying to get away with an empty title'
)
), array('atomic' => false));
), array('validate' => true, 'atomic' => false));

$this->assertIdentical($result, array(true, false));

$result = $TestModel->saveAll(array(
Expand All @@ -2887,7 +2888,7 @@ function testSaveAllAtomic() {
'published' => 'Y',
'user_id' => 2
))
), array('atomic' => false));
), array('validate' => true, 'atomic' => false));
$this->assertIdentical($result, array('Article' => true, 'Comment' => array(true, true)));
}

Expand Down Expand Up @@ -2985,7 +2986,7 @@ function testSaveAllHasManyValidation() {
'Comment' => array(
array('comment' => '', 'published' => 'Y', 'user_id' => 1),
)
));
), array('validate' => true));
$expected = array('Comment' => array(false));
$this->assertEqual($result, $expected);

Expand Down Expand Up @@ -3318,7 +3319,7 @@ function testSaveAllValidation() {
'title' => '',
'body' => 'Trying to get away with an empty title'
));
$result = $TestModel->saveAll($data, array('atomic' => false));
$result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
$this->assertEqual($result, array(true, false));
$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
$errors = array(1 => array('title' => 'This field cannot be left blank'));
Expand Down

0 comments on commit d365faf

Please sign in to comment.