From 5a3bca6c2b373a3be328542c31adf1cb048f6b5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?An=C5=BEe=20=C4=8Casar?= Date: Fri, 13 Nov 2020 10:55:03 +0100 Subject: [PATCH] Alternative version of faster transWhere This PR is an alternative version of https://github.com/rainlab/translate-plugin/pull/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: https://github.com/rainlab/translate-plugin/issues/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. --- behaviors/TranslatableModel.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/behaviors/TranslatableModel.php b/behaviors/TranslatableModel.php index c578b2de..b9ce59c6 100755 --- a/behaviors/TranslatableModel.php +++ b/behaviors/TranslatableModel.php @@ -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; }