From 00b30d095a4ed7eed7a42ad202495db4ee3c955e Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 1 Feb 2016 22:09:06 -0500 Subject: [PATCH] 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 --- src/ORM/Behavior/TranslateBehavior.php | 3 ++ .../ORM/Behavior/TranslateBehaviorTest.php | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/ORM/Behavior/TranslateBehavior.php b/src/ORM/Behavior/TranslateBehavior.php index c28ee667949..0c7ce98ae64 100644 --- a/src/ORM/Behavior/TranslateBehavior.php +++ b/src/ORM/Behavior/TranslateBehavior.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 = []; diff --git a/tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php b/tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php index 533c24b7b3a..9c2036fa32c 100644 --- a/tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php +++ b/tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php @@ -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