Browse Source

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

Jose Lorenzo Rodriguez 12 years ago
parent
commit
0dced17fdf

+ 26 - 10
src/Model/Behavior/TranslateBehavior.php

@@ -367,19 +367,16 @@ class TranslateBehavior extends Behavior {
 			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);
 			}
 		}
@@ -387,4 +384,23 @@ class TranslateBehavior extends Behavior {
 		$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();
+	}
+
 }

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

@@ -610,4 +610,29 @@ class TranslateBehaviorTest extends TestCase {
 		$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'));
+	}
 }