Код: Выделить всё
private function getCitiesName($countryId = null, $cityId = null)
{
if ( isset($countryId) && isset($cityId) ){
// $city = City::getDb()->cache(function ($db) {
if (isset($countryId)){
$city = City::find()
->select('id, slug, country_id')
->where(['country_id' => $countryId])
->asArray()
->indexBy('id')
->all();
}
else{
return null;
}
//});
return $city[$cityId]['slug'];
}
return null;
}
Код: Выделить всё
public function createUrl($manager, $route, $params)
{
if ($route === 'site/index'){
if ( isset($params['country']) ){
$country = $this->getCountryName($params['country']);
if ( isset($params['city']) ){
$city = $this->getCitiesName($params['country'], $params['city']);
if ($country !== null && $city !== null){
return $country . '/' . $city;
}
}
if ($country !== null) {
return $country;
}
}
}
return false;
}
Все дело в том что если у определенной страны много городов, то много и соответствующих запросов к БД, вопрос в том как можно закэшировать этот запрос с передачей параметра?