Skip to content

Commit

Permalink
Moving beforeFind triggering a bit deeper so that it is not fired twice
Browse files Browse the repository at this point in the history
for the same query
  • Loading branch information
lorenzo committed Jan 26, 2014
1 parent b075bfb commit 625c562
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/ORM/Query.php
Expand Up @@ -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();
}

Expand All @@ -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);
}
Expand Down
47 changes: 46 additions & 1 deletion tests/TestCase/Model/Behavior/TranslateBehaviorTest.php
Expand Up @@ -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.