Permalink
Browse files

Making it possible to add new translations and save them in batch

  • Loading branch information...
1 parent 6bfb796 commit 0dced17fdf045b335f27dfefc02ffaca8e51d949 @lorenzo lorenzo committed Feb 15, 2014
Showing with 51 additions and 10 deletions.
  1. +26 −10 src/Model/Behavior/TranslateBehavior.php
  2. +25 −0 tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
@@ -367,24 +367,40 @@ protected function _bundleTranslatedFields($entity) {
return;
}
- $association = $this->_table->association($this->config()['translationTable']);
- $query = $association->find()->select('id')->where(array_shift($find));
- foreach ($find as $conditions) {
- $q = $association->find()->select('id')->where(array_shift($find));
- $query->unionAll($q);
- }
+ $results = $this->_findExistingTranslations($find);
+ $alias = $this->_table->alias();
- foreach ($query->hydrate(false)->bufferResults(false) as $i => $row) {
- if (!empty($row['id'])) {
- $contents[$i]->set('id', $row['id'], ['setter' => false]);
+ foreach ($find as $i => $translation) {
+ if (!empty($results[$i])) {
+ $contents[$i]->set('id', $results[$i], ['setter' => false]);
$contents[$i]->isNew(false);
} else {
- $contents[$i]->set($find[$i], ['setter' => false, 'guard' => false]);
+ $translation['model'] = $alias;
+ $contents[$i]->set($translation, ['setter' => false, 'guard' => false]);
$contents[$i]->isNew(true);
}
}
$entity->set('_i18n', $contents);
}
+ protected function _findExistingTranslations($ruleSet) {
+ $association = $this->_table->association($this->config()['translationTable']);
+ $query = $association->find()
+ ->select(['id', 'num' => 0])
+ ->where(current($ruleSet))
+ ->hydrate(false)
+ ->bufferResults(false);
+
+ unset($ruleSet[0]);
+ foreach ($ruleSet as $i => $conditions) {
+ $q = $association->find()
+ ->select(['id', 'num' => $i])
+ ->where($conditions);
+ $query->unionAll($q);
+ }
+
+ return $query->combine('num', 'id')->toArray();
+ }
+
}
@@ -610,4 +610,29 @@ public function testSaveMultipleTranslations() {
$this->assertEquals('Another body', $translations['eng']->get('body'));
}
+/**
+ * Tests saving multiple existing translations and adding new ones
+ *
+ * @return void
+ */
+ public function testSaveMultipleNewTranslations() {
+ $table = TableRegistry::get('Articles');
+ $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
+ $article = $results = $table->find('translations')->first();
+
+ $translations = $article->get('_translations');
+ $translations['deu']->set('title', 'Another title');
+ $translations['eng']->set('body', 'Another body');
+ $translations['spa'] = new Entity(['title' => 'Titulo']);
+ $translations['fre'] = new Entity(['title' => 'Titre']);
+ $article->set('_translations', $translations);
+ $table->save($article);
+
+ $article = $results = $table->find('translations')->first();
+ $translations = $article->get('_translations');
+ $this->assertEquals('Another title', $translations['deu']->get('title'));
+ $this->assertEquals('Another body', $translations['eng']->get('body'));
+ $this->assertEquals('Titulo', $translations['spa']->get('title'));
+ $this->assertEquals('Titre', $translations['fre']->get('title'));
+ }
}

0 comments on commit 0dced17

Please sign in to comment.