Skip to content
Permalink
Browse files

Preserve Translation data in result formatter.

When results are iterated in a result formatter, the _translations
property would get wiped as the groupTranslations formatter would be
invoked each time the results were iterated.

Refs #8121
  • Loading branch information...
markstory committed Feb 2, 2016
1 parent dcac189 commit 00b30d095a4ed7eed7a42ad202495db4ee3c955e
Showing with 32 additions and 0 deletions.
  1. +3 −0 src/ORM/Behavior/TranslateBehavior.php
  2. +29 −0 tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
@@ -457,6 +457,9 @@ public function groupTranslations($results)
return $row;
}
$translations = (array)$row->get('_i18n');
if (empty($translations) && $row->get('_translations')) {
return $row;
}
$grouped = new Collection($translations);
$result = [];
@@ -137,6 +137,35 @@ public function testFindSingleLocale()
$this->assertSame($expected, $results);
}
/**
* Test that iterating in a formatResults() does not drop data.
*
* @return void
*/
public function testFindTranslationsFormatResultsIteration()
{
$table = TableRegistry::get('Articles');
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
$table->locale('eng');
$results = $table->find('translations')
->limit(1)
->formatResults(function ($results) {
foreach ($results as $res) {
$res->first = 'val';
}
foreach ($results as $res) {
$res->second = 'loop';
}
return $results;
})
->toArray();
$this->assertCount(1, $results);
$this->assertSame('Title #1', $results[0]->title);
$this->assertSame('val', $results[0]->first);
$this->assertSame('loop', $results[0]->second);
$this->assertNotEmpty($results[0]->_translations);
}
/**
* Tests that fields from a translated model use the I18n class locale
* and that it propogates to associated models

0 comments on commit 00b30d0

Please sign in to comment.
You can’t perform that action at this time.