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

Fix withTranslation query scope performance issues and fallback for country-based locales #417

Merged
merged 4 commits into from
Jan 4, 2018

Conversation

raphaelsaunier
Copy link
Contributor

@raphaelsaunier raphaelsaunier commented Nov 17, 2017

This PR fixes two separate but related issues with the withTranslation query scope.

Slow performance due to over-fetching

As reported in #241, the generated SQL has an error that will cause it to incorrectly fetch all of the translations for the default language:

SELECT * FROM `xxx_translations
WHERE `xxx_translations`.`product_id` IN ('59') AND
       `xxx_translations`.`locale` = 'fr' OR `xxx_translations`.`locale` = 'en'`
      ^ parenthesis is missing here                                ... and here ^

This can be fixed by wrapping the OR clauses in parentheses (by using the adequate query builder methods), or simply by using a WHERE IN statement. I chose the latter option.


Language fallback is skipped when using a country-based locale

When using country-based fallbacks, the language fallback wasn't appended to the SQL request. So, to build on the current example scenario in the README, instead of querying for es-MX, es, en, the query only looked for es-MX and en, and skipping the language altogether.

Also, since this feature didn't seem to have any tests, I have added started adding a few.

Tests were failing because the return array also contained an unexpected
'translations' key.
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 raphaelsaunier changed the title Fix/fallback queries Fix withTranslation query scope performance issues and fallback for country-based locales Nov 17, 2017
Copy link
Owner

@dimsav dimsav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! 💅

@dimsav dimsav merged commit 68a0b78 into dimsav:master 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

Successfully merging this pull request may close these issues.

None yet

2 participants