#4803 Ad Model Db Optimization

This commit is contained in:
Muammer Top 2021-10-28 14:52:41 +03:00
parent 82028cb8d4
commit d44896e2a2

View File

@ -287,19 +287,22 @@ class AdvModel extends AdvsAdvsEntryModel implements AdvInterface
public function getLocationNames($advs) public function getLocationNames($advs)
{ {
foreach ($advs as $adv) { foreach ($advs as $adv) {
$country = CountryModel::query()->where('location_countries.id', $adv->country_id)->first(); $locale = config('app.locale', config('streams::locales.default'));
$city = CityModel::query()->where('location_cities.id', $adv->city)->first();
$district = DistrictModel::query()->where('location_districts.id', $adv->district)->first();
if ($country != null) { $country_id = $adv->country_id ?? 0;
$adv->setAttribute('country_name', $country->name); $city_id = $adv->city ?? 0;
$adv->setAttribute('country_abv', $country->abv); $district_id = $adv->district ?? 0;
}
if ($city != null) { $q = collect(DB::select("
$adv->setAttribute('city_name', $city->name); SELECT country.abv as country_abv, country_trans.name as country_name,
} (SELECT name FROM default_location_cities_translations as city_trans WHERE city_trans.id = " . $city_id . " AND city_trans.locale = '" . $locale . "') as city_name,
if ($district != null) { (SELECT name FROM default_location_districts_translations as district_trans WHERE district_trans.id = " . $district_id . " AND district_trans.locale = '" . $locale . "') as district_name
$adv->setAttribute('district_name', $district->name); FROM default_location_countries AS country
JOIN default_location_countries_translations AS country_trans on country.id = country_trans.entry_id WHERE country.id = " . $country_id . " and country_trans.locale = '" . $locale . "'
"))->first();
foreach ($q as $key => $value){
$adv->setAttribute($key, $value);
} }
} }
return $advs; return $advs;