-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User - Profile - Country Field without value #6919
Comments
@yurabakhtin We should only store the country code in the database and display the localized country name (if possible). One problem, when storing the code, could be the search for a country name. |
Yes, after my current fix we store in DB only a country code, and it worked this way before the changes from the PR #6809.
Yes, it is a problem, because there is a simple SQL query like |
@luke- However we cannot store a full country name in DB because the names are translatable. Let me know if I should try to implement a solution like this: $countryCodes = [$keyword];
foreach (Iso3166Codes::$countriesas $code => $value) {
if (stripos(Iso3166Codes::country($code), $keyword) !== false) {
$countryCodes[] = $code;
}
}
$query->andWhere(['IN', 'profile.country', $countryCodes]); |
@yurabakhtin Ok perfect. I just wanted to make sure that we really only save the country code. Then that's fine. Would be cool if you could try this wordaround, then we would have solved the search issue. |
@yurabakhtin Thanks for the fix. |
@marc-farre Good question! I see a full country name was stored in DB from |
@yurabakhtin Maybe we could do a migration for the default language only? If not, as I have to do it for some instances I manage, I'll share the code here. |
@marc-farre Ideally we should do some update in a migration by single query like $profiles = Profile::find()
->select('country')
->distinct('country')
->where(['NOT IN', 'country', array_keys(Iso3166Codes::$countries)])
->andWhere(['IS NOT', 'country', new Expression('NULL')]);
foreach ($profiles->column() as $wrongCountryCode) {
Profile::updateAll(
['country' => $this->getCodeByCountry($wrongCountryCode)],
['country' => $wrongCountryCode]);
} public function getCodeByCountry($wrongCountryCode): ?string
{
if ($this->translatedCountries === null) {
$this->translatedCountries = [];
foreach (Iso3166Codes::$countries as $code => $title) {
$this->translatedCountries[$code] = [];
foreach (Yii::$app->params['availableLanguages'] as $language => $name) {
$this->translatedCountries[$code][] = \Locale::getDisplayRegion('_' . $code, $language);
}
}
}
foreach ($this->translatedCountries as $code => $titles) {
if (in_array($wrongCountryCode, $titles)) {
return $code;
}
}
return null;
} So the array The array @luke- Do you agree? |
@yurabakhtin Thanks very much! Tested, it works fine. |
@yurabakhtin Looks good for me. Should be a good approach which also scales... |
@marc-farre Have you already started a PR for the migration code or should I do this? |
The text was updated successfully, but these errors were encountered: