#6935 Fix deep saving for hasMany could not handle validation errors correctly

This commit is contained in:
hiromi2424 2015-07-04 06:10:58 +00:00
parent c037e04ace
commit deb62c8887
3 changed files with 172 additions and 3 deletions

View file

@ -2339,7 +2339,7 @@ class Model extends Object implements CakeEventListener {
}
}
$validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, $saved, true))));
$validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, Hash::flatten($saved), true))));
if (!$validates) {
$validationErrors[$key] = $this->validationErrors;
}
@ -2480,7 +2480,7 @@ class Model extends Object implements CakeEventListener {
} else {
$saved = $Model->save($values, array('atomic' => false) + $options);
}
$validates = ($saved === true || (is_array($saved) && !in_array(false, $saved, true)));
$validates = ($saved === true || (is_array($saved) && !in_array(false, Hash::flatten($saved), true)));
}
if ($validates) {
@ -2538,7 +2538,7 @@ class Model extends Object implements CakeEventListener {
}
}
$validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, $saved, true))));
$validates = ($validates && ($saved === true || (is_array($saved) && !in_array(false, Hash::flatten($saved), true))));
if (!$validates) {
$validationErrors[$association] = $Model->validationErrors;
}

View file

@ -7684,6 +7684,128 @@ class ModelWriteTest extends BaseModelTest {
$this->assertEquals(2, count($result['Attachment']));
}
/**
* testSaveManyDeepHasManyValidationFailure method
*
* @return void
*/
public function testSaveManyDeepHasManyValidationFailure() {
$this->loadFixtures('Article', 'Comment');
$TestModel = new Article();
$TestModel->Comment->validate = array(
'comment' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
)
)
);
$result = $TestModel->saveMany(array(
array(
'user_id' => 1,
'title' => 'New Article',
'body' => 'This article contains a invalid comment',
'Comment' => array(
array(
'user_id' => 1,
'comment' => ''
)
)
)
), array('deep' => true));
$this->assertFalse($result);
$this->assertEquals(array(
array(
'Comment' => array(
array('comment' => array('notEmpty'))
)
)
), $TestModel->validationErrors);
}
/**
* testSaveAssociatedDeepHasOneHasManyValidateTrueValidationFailure method
*
* @return void
*/
public function testSaveAssociatedDeepHasOneHasManyValidateTrueValidationFailure() {
$this->loadFixtures('User', 'Article', 'Comment');
$TestModel = new UserHasOneArticle();
$TestModel->Article->Comment->validate = array(
'comment' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
)
)
);
$result = $TestModel->saveAssociated(array(
'User' => array(
'user' => 'hiromi',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
),
'Article' => array(
'title' => 'Article with User',
'body' => 'This article will be saved with an user and contains a invalid comment',
'Comment' => array(
array(
'user_id' => 1,
'comment' => ''
)
)
)
), array('deep' => true, 'validate' => true));
$this->assertFalse($result);
$this->assertEquals(array(
'Article' => array(
'Comment' => array(
array('comment' => array('notEmpty'))
)
)
), $TestModel->validationErrors);
}
/**
* testSaveAssociatedDeepBelongsToHasManyValidateTrueValidationFailure method
*
* @return void
*/
public function testSaveAssociatedDeepBelongsToHasManyValidateTrueValidationFailure() {
$this->loadFixtures('ArticlesTag', 'Article', 'Comment');
$TestModel = new ArticlesTagBelongsToArticle();
$TestModel->Article->Comment->validate = array(
'comment' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
)
)
);
$result = $TestModel->saveAssociated(array(
'ArticlesTagBelongsToArticle' => array(
'tag_id' => 1,
),
'Article' => array(
'title' => 'Article with User',
'body' => 'This article will be saved with an user and contains a invalid comment',
'Comment' => array(
array(
'user_id' => 1,
'comment' => ''
)
)
)
), array('deep' => true, 'validate' => true));
$this->assertFalse($result);
$this->assertEquals(array(
'Article' => array(
'Comment' => array(
array('comment' => array('notEmpty'))
)
)
), $TestModel->validationErrors);
}
/**
* testUpdateAllBoolean
*

View file

@ -5079,3 +5079,50 @@ class Example extends AppModel {
);
}
/**
* UserHasOneArticle class
*
* @package Cake.Test.Case.Model
*/
class UserHasOneArticle extends AppModel {
/**
* useTable property
*
* @var string
*/
public $useTable = 'users';
/**
* hasOne property
*
* @var array
*/
public $hasOne = array('Article');
}
/**
* ArticlesTagBelongsToArticle class
*
* @package Cake.Test.Case.Model
*/
class ArticlesTagBelongsToArticle extends CakeTestModel {
/**
* useTable property
*
* @var string
*/
public $useTable = 'articles_tags';
/**
* belongsTo property
*
* @var array
*/
public $belongsTo = array('Article');
}