Browse Source

not overwritting fields with a null translation

Jose Lorenzo Rodriguez 12 years ago
parent
commit
6fe3d92de8

+ 5 - 1
src/Model/Behavior/TranslateBehavior.php

@@ -219,7 +219,11 @@ class TranslateBehavior extends Behavior {
 					continue;
 				}
 
-				$row->set($field, $translation->get('content'), $options);
+				$content = $translation->get('content');
+				if ($content !== null) {
+					$row->set($field, $content, $options);
+				}
+
 				unset($row[$name]);
 			}
 

+ 17 - 0
tests/TestCase/Model/Behavior/TranslateBehaviorTest.php

@@ -79,6 +79,23 @@ class TranslateBehaviorTest extends TestCase {
 	}
 
 /**
+ * Tests that fields from a translated model are not overriden if translation
+ * is null
+ *
+ * @return void
+ */
+	public function testFindSingleLocaleWithNullTranslation() {
+		$table = TableRegistry::get('Comments');
+		$table->addBehavior('Translate', ['fields' => ['comment']]);
+		$table->locale('spa');
+		$results = $table->find()
+			->where(['Comments.id' => 6])
+			->combine('id', 'comment')->toArray();
+		$expected = [6 => 'Second Comment for Second Article'];
+		$this->assertSame($expected, $results);
+	}
+
+/**
  * Tests that overriding fields with the translate behavior works when
  * using conditions and that all other columns are preserved
  *