diff --git a/src/Model/Behavior/TranslateBehavior.php b/src/Model/Behavior/TranslateBehavior.php index a79be02ae85..6288cddb4de 100644 --- a/src/Model/Behavior/TranslateBehavior.php +++ b/src/Model/Behavior/TranslateBehavior.php @@ -219,7 +219,11 @@ protected function _rowMapper($results, $locale) { continue; } - $row->set($field, $translation->get('content'), $options); + $content = $translation->get('content'); + if ($content !== null) { + $row->set($field, $content, $options); + } + unset($row[$name]); } diff --git a/tests/TestCase/Model/Behavior/TranslateBehaviorTest.php b/tests/TestCase/Model/Behavior/TranslateBehaviorTest.php index 0161ad500d6..092fca5ecfc 100644 --- a/tests/TestCase/Model/Behavior/TranslateBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/TranslateBehaviorTest.php @@ -78,6 +78,23 @@ public function testFindSingleLocale() { $this->assertSame($expected, $results); } +/** + * 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