Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

scopeWithTranslation where concatenation #241

Closed
maurocordioli opened this issue May 9, 2016 · 0 comments
Closed

scopeWithTranslation where concatenation #241

maurocordioli opened this issue May 9, 2016 · 0 comments

Comments

@maurocordioli
Copy link

hi i have noticed that in scopeWithTranslation the second where for the default language is appended to the main query causing the query emitted by eloquent be similar to
*{external_id_field} in (x,y,z....) and locale={currentlocale} or locale={defaultlocale} . *
without parentization the correct query should be some thing like
*{external_id_field} in (x,y,z....) and( locale={current_locale} or locale={default_locale}) . *
using the wrong query in practice does not came with a wrong final result because the master records are filtered, but the performance are very bad ...

you'll find a possible correction of the query scope below

 /**
     * This scope eager loads the translations for the default and the fallback locale only.
     * We can use this as a shortcut to improve performance in our application.
     *
     * @param Builder $query
     */
    public function scopeWithTranslation(Builder $query)
    {
        $query->with(['translations' => function(Relation $q){
            $q->where(function ($query) {
            $query->where($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->locale());

            if ($this->useFallback()) {
                return $query->orWhere($this->getTranslationsTable().'.'.$this->getLocaleKey(), $this->getFallbackLocale());
            }
            });

        }]);
    }
raphaelsaunier added a commit to raphaelsaunier/laravel-translatable that referenced this issue Nov 17, 2017
When using the `withTranslation` query scope along with country-based
locales, the generated query didn't include the country fallback (e.g.
`de-DE` should first fall back to `de` before using the fallback defined
in the configuration).

Also fix dimsav#241 by generating a WHERE IN clause instead chaining AND/ORs
raphaelsaunier added a commit to raphaelsaunier/laravel-translatable that referenced this issue Nov 17, 2017
When using the `withTranslation` query scope along with country-based
locales, the generated query didn't include the country fallback (e.g.
`de-DE` should first fall back to `de` before using the fallback defined
in the configuration).

Also fix dimsav#241 by generating a WHERE IN clause instead chaining AND/ORs
raphaelsaunier added a commit to raphaelsaunier/laravel-translatable that referenced this issue Nov 17, 2017
When using the `withTranslation` query scope along with country-based
locales, the generated query didn't include the country fallback (e.g.
`de-DE` should first fall back to `de` before using the fallback defined
in the configuration).

Also fix dimsav#241 by generating a WHERE IN clause instead chaining AND/ORs
@dimsav dimsav closed this as completed in 68a0b78 Jan 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant