Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improving save all translations

  • Loading branch information...
commit 6bfb79656318bdcd6785f2e0473ea0d11547cb1c 1 parent e3c730d
José Lorenzo Rodríguez lorenzo authored
27 src/Model/Behavior/TranslateBehavior.php
View
@@ -342,7 +342,7 @@ protected function _groupTranslations($results) {
protected function _bundleTranslatedFields($entity) {
$translations = (array)$entity->get('_translations');
- if (empty($translations)) {
+ if (empty($translations) && !$entity->dirty('_translations')) {
return;
}
@@ -353,14 +353,13 @@ protected function _bundleTranslatedFields($entity) {
foreach ($translations as $lang => $translation) {
foreach ($fields as $field) {
- if ($translation->dirty($field)) {
- $find[] = [
- 'locale' => $lang,
- 'field' => $field,
- 'foreign_key' => $key
- ];
- $contents[] = $translation->get($field);
+ if (!$translation->dirty($field)) {
+ continue;
}
+ $find[] = ['locale' => $lang, 'field' => $field, 'foreign_key' => $key];
+ $contents[] = new Entity(['content' => $translation->get($field)], [
+ 'useSetters' => false
+ ]);
}
}
@@ -375,9 +374,17 @@ protected function _bundleTranslatedFields($entity) {
$query->unionAll($q);
}
- foreach ($query->hydrate(false)->bufferResults(false) as $row) {
- debug($row);
+ foreach ($query->hydrate(false)->bufferResults(false) as $i => $row) {
+ if (!empty($row['id'])) {
+ $contents[$i]->set('id', $row['id'], ['setter' => false]);
+ $contents[$i]->isNew(false);
+ } else {
+ $contents[$i]->set($find[$i], ['setter' => false, 'guard' => false]);
+ $contents[$i]->isNew(true);
+ }
}
+
+ $entity->set('_i18n', $contents);
}
}
6 tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
View
@@ -587,6 +587,12 @@ public function testDelete() {
$this->assertEquals(0, $translations);
}
+/**
+ * Tests saving multiple translations at once when the translations already
+ * exist in the database
+ *
+ * @return void
+ */
public function testSaveMultipleTranslations() {
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
Please sign in to comment.
Something went wrong with that request. Please try again.