Skip to content

Commit

Permalink
Alternative version of faster transWhere
Browse files Browse the repository at this point in the history
This PR is an alternative version of rainlab#586

Instead of creating a separate method that has a different behavior to the original implementation, I managed to fix ``transWhere`` itself by utilizing 2 queries instead of the slow orWhere clause. This implementation also preserves the fallback functionality.

I made some tests on my project (originally discussed here: rainlab#581) - now I have 2 queries that execute in ~ 5 ms instead of a single 60 second query. The improvement is significant.

Comments are welcome.
  • Loading branch information
acasar authored Nov 13, 2020
1 parent 535883c commit 5a3bca6
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions behaviors/TranslatableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@ public function __construct($model)
*/
public function scopeTransWhere($query, $index, $value, $locale = null, $operator = '=')
{
$query->select($this->model->getTable().'.*');

$query->where(function($q) use ($index, $value, $operator) {
$q->where($this->model->getTable().'.'.$index, $operator, $value);
$q->orWhere(function($q) use ($index, $value, $operator) {
$q
->where('rainlab_translate_indexes.item', $index)
->where('rainlab_translate_indexes.value', $operator, $value)
;
});
});
if (!$locale) {
$locale = $this->translatableContext;
}

$this->joinTranslateIndexesTable($query, $locale);
$translateIndexes = DB::table('rainlab_translate_indexes')
->where('rainlab_translate_indexes.model_type', '=', $this->getClass())
->where('rainlab_translate_indexes.locale', '=', $locale)
->where('rainlab_translate_indexes.item', $index)
->where('rainlab_translate_indexes.value', $operator, $value)
->pluck('model_id');

if($translateIndexes->count()) {
$query->whereIn($this->model->getQualifiedKeyName(), $translateIndexes);
} else {
$query->where($index, $operator, $value);
}

return $query;
}
Expand Down

0 comments on commit 5a3bca6

Please sign in to comment.