Skip to content

Commit

Permalink
Merge pull request #8158 from cakephp/issue-8121
Browse files Browse the repository at this point in the history
Preserve Translation data in result formatter.
  • Loading branch information
lorenzo committed Feb 2, 2016
2 parents dcac189 + 00b30d0 commit 006c5a3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/ORM/Behavior/TranslateBehavior.php
Expand Up @@ -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 = [];
Expand Down
29 changes: 29 additions & 0 deletions tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
Expand Up @@ -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
Expand Down

0 comments on commit 006c5a3

Please sign in to comment.