Browse Source

Merge branch 'master' into 2.3

mark_story 13 years ago
parent
commit
ed19821168

+ 5 - 2
lib/Cake/Model/Behavior/TranslateBehavior.php

@@ -396,9 +396,12 @@ class TranslateBehavior extends ModelBehavior {
 
 		$fields = array_merge($this->settings[$model->alias], $this->runtime[$model->alias]['fields']);
 		if ($created) {
-			foreach ($fields as $field) {
+			// set each field value to an empty string
+			foreach ($fields as $key => $field) {
+				if (!is_numeric($key)) {
+					$field = $key;
+				}
 				if (!isset($tempData[$field])) {
-					//set the field value to an empty string
 					$tempData[$field] = '';
 				}
 			}

+ 32 - 5
lib/Cake/Test/Case/Model/Behavior/TranslateBehaviorTest.php

@@ -1,9 +1,5 @@
 <?php
 /**
- * TranslateBehaviorTest file
- *
- * PHP 5
- *
  * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -12,7 +8,6 @@
  *
  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @package       Cake.Test.Case.Model.Behavior
  * @since         CakePHP(tm) v 1.2.0.5669
  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
@@ -1058,4 +1053,36 @@ class TranslateBehaviorTest extends CakeTestCase {
 		$this->assertNotContains('slug', $result);
 	}
 
+/**
+ * Test that additional records are not inserted for associated translations.
+ *
+ * @return void
+ */
+	public function testNoExtraRowsForAssociatedTranslations() {
+		$this->loadFixtures('Translate', 'TranslatedItem');
+		$TestModel = new TranslatedItem();
+		$TestModel->locale = 'spa';
+		$TestModel->unbindTranslation();
+		$TestModel->bindTranslation(array('name' => 'nameTranslate'));
+
+		$data = array(
+			'TranslatedItem' => array(
+				'slug' => 'spanish-name',
+				'name' => 'Spanish name',
+			),
+		);
+		$TestModel->create($data);
+		$TestModel->save();
+
+		$Translate = $TestModel->translateModel();
+		$results = $Translate->find('all', array(
+			'conditions' => array(
+				'locale' => $TestModel->locale,
+				'foreign_key' => $TestModel->id
+			)
+		));
+		$this->assertCount(1, $results, 'Only one field should be saved');
+		$this->assertEquals('name', $results[0]['TranslateTestModel']['field']);
+	}
+
 }