Browse Source

improve saveAll modification

euromark 12 years ago
parent
commit
f31e246c0b
3 changed files with 25 additions and 4 deletions
  1. 8 3
      Model/MyModel.php
  2. 1 1
      Test/Case/Lib/AuthTest.php
  3. 16 0
      Test/Case/Model/MyModelTest.php

+ 8 - 3
Model/MyModel.php

@@ -392,7 +392,12 @@ class MyModel extends Model {
 
 	/**
 	 * Fix for non atomic queries (MyISAM  etc) and saveAll to still return just the boolean result
-	 * Otherwise you would have to interate over all result values to find out if the save was successful.
+	 * Otherwise you would have to iterate over all result values to find out if the save was successful.
+	 *
+	 * Use Configure::read('Model.atomic') to modify atomic behavior.
+	 * Additional options:
+	 *
+	 * - returnArray: bool
 	 *
 	 * @param mixed $data
 	 * @param array $options
@@ -400,12 +405,12 @@ class MyModel extends Model {
 	 * 2012-11-10 ms
 	 */
 	public function saveAll($data = null, $options = array()) {
-		if (!isset($options['atomic'])) {
+		if (!isset($options['atomic']) && Configure::read('Model.atomic') !== null) {
 			$options['atomic'] = (bool)Configure::read('Model.atomic');
 		}
 		$res = parent::saveAll($data, $options);
 
-		if (is_array($res)) {
+		if (is_array($res) && empty($options['returnArray'])) {
 			$res = Utility::isValidSaveAll($res);
 		}
 		return $res;

+ 1 - 1
Test/Case/Lib/AuthTest.php

@@ -12,7 +12,7 @@ class AuthTest extends MyCakeTestCase {
 	public function setUp() {
 		parent::setUp();
 
-		ClassRegistry::init('Session');
+		ClassRegistry::init(array('table' => 'cake_sessions', 'class' => 'Session', 'alias' => 'Session'));
 
 		$this->skipIf(php_sapi_name() === 'cli', 'Cannot test session in CLI');
 	}

+ 16 - 0
Test/Case/Model/MyModelTest.php

@@ -118,6 +118,22 @@ class MyModelTest extends MyCakeTestCase {
 		$this->assertSame('`User`.`dob`', $res);
 	}
 
+	public function testSaveAll() {
+		$records = array(
+			array('title' => 'x', 'body' => 'bx'),
+			array('title' => 'y', 'body' => 'by'),
+		);
+		$result = $this->App->saveAll($records);
+		$this->assertTrue($result);
+
+		$result = $this->App->saveAll($records, array('atomic' => false));
+		$this->assertTrue($result);
+
+		$result = $this->App->saveAll($records, array('atomic' => false, 'returnArray' => true));
+		$expected = array(true, true);
+		$this->assertSame($expected, $result);
+	}
+
 	/**
 	 * Test deleteAllRaw()
 	 *