ソースを参照

Fix saveAssociated() with validate=first, atomic=false

When using the above options & validation errors on the associated
models, saving would not be aborted.

Fixes #3285
mark_story 13 年 前
コミット
08556ab879
2 ファイル変更46 行追加1 行削除
  1. 1 1
      lib/Cake/Model/Model.php
  2. 45 0
      lib/Cake/Test/Case/Model/ModelWriteTest.php

+ 1 - 1
lib/Cake/Model/Model.php

@@ -2183,7 +2183,7 @@ class Model extends Object implements CakeEventListener {
 
 		if ($options['validate'] === 'first') {
 			$validates = $this->validateAssociated($data, $options);
-			if ((!$validates && $options['atomic']) || (!$options['atomic'] && in_array(false, $validates, true))) {
+			if ((!$validates && $options['atomic']) || (!$options['atomic'] && in_array(false, Hash::flatten($validates), true))) {
 				return $validates;
 			}
 			$options['validate'] = false;

+ 45 - 0
lib/Cake/Test/Case/Model/ModelWriteTest.php

@@ -4831,6 +4831,51 @@ class ModelWriteTest extends BaseModelTest {
 	}
 
 /**
+ * Test that validate = first, atomic = false works when associated records
+ * fail validation.
+ *
+ * @return void
+ */
+	public function testSaveAssociatedAtomicFalseValidateFirstWithErrors() {
+		$this->loadFixtures('Comment', 'Article', 'User');
+		$Article = ClassRegistry::init('Article');
+		$Article->Comment->validator()->add('comment', array(
+			array('rule' => 'notEmpty')
+		));
+
+		$data = array(
+			'Article' => array(
+				'user_id' => 1,
+				'title' => 'Foo',
+				'body' => 'text',
+				'published' => 'N'
+			),
+			'Comment' => array(
+				array(
+					'user_id' => 1,
+					'comment' => '',
+					'published' => 'N',
+				)
+			),
+		);
+
+		$Article->saveAssociated(
+			$data,
+			array('validate' => 'first', 'atomic' => false)
+		);
+
+		$result = $Article->validationErrors;
+		$expected = array(
+			'Comment' => array(
+				array(
+					'comment' => array( 'This field cannot be left blank' )
+				)
+			)
+		);
+		$this->assertEquals($expected, $result);
+	}
+
+/**
  * testSaveMany method
  *
  * @return void