Skip to content

Commit

Permalink
Adds scopeTransWhereNoFallback
Browse files Browse the repository at this point in the history
This complements scopeTransWhere providing an option to disable the fallback query
  • Loading branch information
samgeorges committed Jul 2, 2022
1 parent 0bfce44 commit fdec343
Showing 1 changed file with 39 additions and 6 deletions.
45 changes: 39 additions & 6 deletions behaviors/TranslatableModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ protected function extendFileModels(string $relationGroup)
}

/**
* Applies a translatable index to a basic query. This scope will join the index
* table and can be executed neither more than once, nor with scopeTransOrder.
* scopeTransWhere applies a translatable index to a basic query. This scope will join the
* index table and can be executed neither more than once, nor with scopeTransOrder.
* @param Builder $query
* @param string $index
* @param string $value
Expand All @@ -76,22 +76,55 @@ protected function extendFileModels(string $relationGroup)
*/
public function scopeTransWhere($query, $index, $value, $locale = null, $operator = '=')
{
return $this->transWhereInternal($query, $index, $value, [
'locale' => $locale,
'operator' => $operator
]);
}

/**
* scopeTransWhereNoFallback is identical to scopeTransWhere except it will not
* use a fallback query when there are no idexes found.
* @see scopeTransWhere
*/
public function scopeTransWhereNoFallback($query, $index, $value, $locale = null, $operator = '=')
{
return $this->transWhereInternal($query, $index, $value, [
'locale' => $locale,
'operator' => $operator,
'noFallback' => true
]);
}

/**
* transWhereInternal
* @link https://github.com/rainlab/translate-plugin/pull/623
*/
protected function transWhereInternal($query, $index, $value, $options = [])
{
extract(array_merge([
'locale' => null,
'operator' => '=',
'noFallback' => false
], $options));

if (!$locale) {
$locale = $this->translatableContext;
}

// Separate query into two separate queries for improved performance
// @see https://github.com/rainlab/translate-plugin/pull/623
$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');
->pluck('model_id')
;

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

Expand Down

0 comments on commit fdec343

Please sign in to comment.