From 8935530d527f8a701c985457ba678156cd69c708 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Mon, 10 Feb 2014 21:42:09 +0100 Subject: [PATCH] Implemented translations saving in the TranslateBehavior --- src/Model/Behavior/TranslateBehavior.php | 37 +++++++++++++++++++ .../Model/Behavior/TranslateBehaviorTest.php | 35 ++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/src/Model/Behavior/TranslateBehavior.php b/src/Model/Behavior/TranslateBehavior.php index 6288cddb4de..d1b66161ffc 100644 --- a/src/Model/Behavior/TranslateBehavior.php +++ b/src/Model/Behavior/TranslateBehavior.php @@ -148,6 +148,43 @@ public function beforeFind(Event $event, $query) { }, $query::PREPEND); } + public function beforeSave(Event $event, $entity) { + $locale = $this->locale(); + + if (!$locale) { + return; + } + + $values = $entity->extract($this->config()['fields'], true); + $fields = array_keys($values); + $key = $entity->get(current((array)$this->_table->primaryKey())); + + $preexistent = TableRegistry::get('I18n')->find() + ->select(['id', 'field']) + ->where(['field IN' => $fields, 'locale' => $locale, 'foreign_key' => $key]) + ->indexBy('field'); + + $modified = []; + foreach ($preexistent as $field => $translation) { + $translation->set('content', $values[$field]); + $modified[$field] = $translation; + } + + $new = array_diff_key($values, $modified); + foreach ($new as $field => $content) { + $new[$field] = new Entity(compact('locale', 'field', 'content'), [ + 'useSetters' => false, + 'markNew' => true + ]); + } + + $entity->set('_i18n', array_values($modified + $new)); + + foreach ($fields as $field) { + $entity->dirty($field, false); + } + } + /** * Sets all future finds for the bound table to also fetch translated fields for * the passed locale. If no value is passed, it returns the currently configured diff --git a/tests/TestCase/Model/Behavior/TranslateBehaviorTest.php b/tests/TestCase/Model/Behavior/TranslateBehaviorTest.php index 22ff697f57c..053f8af4a23 100644 --- a/tests/TestCase/Model/Behavior/TranslateBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/TranslateBehaviorTest.php @@ -456,4 +456,39 @@ public function testFindSingleLocaleBelongsto() { $this->assertEquals($expected, $results); } +/** + * Tests that updating an existing record translations work + * + * @return void + */ + public function testUpdateSingleLocale() { + $table = TableRegistry::get('Articles'); + $table->addBehavior('Translate', ['fields' => ['title', 'body']]); + $table->locale('eng'); + $article = $table->find()->first(); + $this->assertEquals(1, $article->get('id')); + $article->set('title', 'New translated article'); + $table->save($article); + + $article = $table->find()->first(); + $this->assertEquals(1, $article->get('id')); + $this->assertEquals('New translated article', $article->get('title')); + $this->assertEquals('Content #1', $article->get('body')); + + $table->locale(false); + $article = $table->find()->first(); + $this->assertEquals(1, $article->get('id')); + $this->assertEquals('First Article', $article->get('title')); + + $table->locale('eng'); + $article->set('title', 'Wow, such translated article'); + $article->set('body', 'A translated body'); + $table->save($article); + + $article = $table->find()->first(); + $this->assertEquals(1, $article->get('id')); + $this->assertEquals('Wow, such translated article', $article->get('title')); + $this->assertEquals('A translated body', $article->get('body')); + } + }