Permalink
Browse files

Moving beforeFind triggering a bit deeper so that it is not fired twice

for the same query
  • Loading branch information...
lorenzo committed Jan 25, 2014
1 parent b075bfb commit 625c56252449bcb5def374f4dba411af29f51e86
Showing with 55 additions and 4 deletions.
  1. +9 −3 src/ORM/Query.php
  2. +46 −1 tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
View
@@ -611,9 +611,6 @@ public function all() {
'You cannot call all() on a non-select query. Use execute() instead.'
);
}
- $table = $this->repository();
- $event = new Event('Model.beforeFind', $table, [$this, $this->_options]);
- $table->getEventManager()->dispatch($event);
return $this->getResults();
}
@@ -629,6 +626,15 @@ public function getResults() {
if (isset($this->_results)) {
return $this->_results;
}
+
+ $table = $this->repository();
+ $event = new Event('Model.beforeFind', $table, [$this, $this->_options]);
+ $table->getEventManager()->dispatch($event);
+
+ if (isset($this->_results)) {
+ return $this->_results;
+ }
+
if ($this->_cache) {
$results = $this->_cache->fetch($this);
}
@@ -231,5 +231,50 @@ public function testFindTranslationsList() {
$this->assertEquals($expected, $results->toArray());
}
-}
+/**
+ * Tests that you can both override fields and find all translations
+ *
+ * @return void
+ */
+ public function testFindTranslationsWithFieldOverriding() {
+ $table = TableRegistry::get('Articles');
+ $table->addBehavior('Translate', ['fields' => ['title', 'body']]);
+ $table->locale('cze');
+ $results = $table->find('translations', ['locales' => ['deu', 'cze']]);
+ $expected = [
+ [
+ 'deu' => ['title' => 'Titel #1', 'body' => 'Inhalt #1', 'locale' => 'deu'],
+ 'cze' => ['title' => 'Titulek #1', 'body' => 'Obsah #1', 'locale' => 'cze']
+ ],
+ [
+ 'deu' => ['title' => 'Titel #2', 'body' => 'Inhalt #2', 'locale' => 'deu'],
+ 'cze' => ['title' => 'Titulek #2', 'body' => 'Obsah #2', 'locale' => 'cze']
+ ],
+ [
+ 'deu' => ['title' => 'Titel #3', 'body' => 'Inhalt #3', 'locale' => 'deu'],
+ 'cze' => ['title' => 'Titulek #3', 'body' => 'Obsah #3', 'locale' => 'cze']
+ ]
+ ];
+
+ $translations = $results->map(function($row) {
+ $translations = $row->get('_translations');
+ if (!$translations) {
+ return [];
+ }
+ return array_map(function($t) {
+ return $t->toArray();
+ }, $translations);
+ });
+ $this->assertEquals($expected, $translations->toArray());
+
+ $expected = [
+ 1 => ['Titulek #1' => 'Obsah #1'],
+ 2 => ['Titulek #2' => 'Obsah #2'],
+ 3 => ['Titulek #3' => 'Obsah #3']
+ ];
+ $grouped = $results->combine('title', 'body', 'id');
+ $this->assertEquals($expected, $grouped->toArray());
+ }
+
+}

0 comments on commit 625c562

Please sign in to comment.