Browse Source

Fixed problem in translate behavior when all translate fields are clean

Fixes #7728
Jose Lorenzo Rodriguez 10 years ago
parent
commit
aa57d64e81

+ 5 - 0
src/ORM/Behavior/TranslateBehavior.php

@@ -276,6 +276,11 @@ class TranslateBehavior extends Behavior
 
         $values = $entity->extract($this->_config['fields'], true);
         $fields = array_keys($values);
+
+        if (empty($fields)) {
+            return;
+        }
+
         $primaryKey = (array)$this->_table->primaryKey();
         $key = $entity->get(current($primaryKey));
         $model = $this->_config['referenceName'];

+ 19 - 0
tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php

@@ -1019,4 +1019,23 @@ class TranslateBehaviorTest extends TestCase
         $result = $table->find()->first();
         $this->assertNull($result->description);
     }
+
+    /**
+     * Test save with clean translate fields
+     *
+     * @return void
+     */
+    public function testSaveWithCleanFields()
+    {
+        $table = TableRegistry::get('Articles');
+        $table->addBehavior('Translate', ['fields' => ['title']]);
+        $table->entityClass(__NAMESPACE__ . '\Article');
+        I18n::locale('fra');
+        $article = $table->get(1);
+        $article->set('body', 'New Body');
+        $table->save($article);
+        $result = $table->get(1);
+        $this->assertEquals('New Body', $result->body);
+        $this->assertSame($article->title, $result->title);
+    }
 }