mhunesi / yii2-csc
Yii2 Country State City migration and models.
Requires
- yiisoft/yii2: ~2.0.0
README
Yii2 extension for Country, State, and City database with migration and Active Record models.
This extension provides comprehensive geographical data including countries, states/provinces, and cities with their related information such as ISO codes, phone codes, currencies, timezones, coordinates, and more.
Data Source
The geographical data used in this extension is sourced from the Countries States Cities Database repository, specifically from version 3.1.
This database includes:
- Countries: 250+ countries with ISO2, ISO3, phone codes, currencies, timezones, and geographical coordinates
- States: 5000+ states/provinces with country relationships and location data
- Cities: 150,000+ cities with state and country relationships
Installation
The preferred way to install this extension is through composer.
Either run:
composer require --prefer-dist mhunesi/yii2-csc "*"
or add to the require section of your composer.json file:
"mhunesi/yii2-csc": "*"
Usage
Configuration
The extension automatically registers translations via Bootstrap. If you need to manually configure translations, add this to your application config:
'components' => [ 'i18n' => [ 'translations' => [ 'csc' => [ 'class' => 'yii\i18n\PhpMessageSource', 'sourceLanguage' => 'en-US', 'basePath' => '@mhunesi/csc/messages', 'fileMap' => [ 'csc' => 'csc.php', ], ], ], ], ],
Supported languages:
- English (en)
- Turkish (tr)
Running Migration
After installation, configure your console application to include the migration path:
'controllerMap' => [ 'migrate' => [ 'class' => 'yii\console\controllers\MigrateController', 'migrationPath' => [ '@app/migrations', '@mhunesi/csc/migrations' ] ] ],
Then run the migration:
php yii migrate
Or directly specify the migration path:
yii migrate --migrationPath=@mhunesi/csc/migrations
This will create three tables:
country- Countries with ISO codes, currencies, timezones, etc.state- States/provinces with country relationshipscity- Cities with state and country relationships
Models
The extension provides three Active Record models:
mhunesi\csc\models\Countrymhunesi\csc\models\Statemhunesi\csc\models\City
Usage Examples
use mhunesi\csc\models\Country; use mhunesi\csc\models\State; use mhunesi\csc\models\City; // Get all countries $countries = Country::find()->all(); // Find a specific country by ISO2 code $turkey = Country::find()->where(['iso2' => 'TR'])->one(); // Get all states of a country $states = State::find()->where(['country_code' => 'TR'])->all(); // Access country's states through relation $country = Country::findOne(1); foreach ($country->states as $state) { echo $state->name . "\n"; // Access state's cities through relation foreach ($state->cities as $city) { echo " - " . $city->name . "\n"; } } // Find cities by country $turkishCities = City::find() ->where(['country_code' => 'TR']) ->all(); // Find a specific city $istanbul = City::find() ->where(['name' => 'Istanbul', 'country_code' => 'TR']) ->one();
Database Schema
Country Table
id- Primary keyname- Country nameiso2- ISO2 code (2 characters)iso3- ISO3 code (3 characters)numeric_code- Numeric country codephone_code- International phone codecapital- Capital citycurrency- Currency codecurrency_name- Currency namecurrency_symbol- Currency symboltld- Top-level domainnative- Native country nameregion- Geographic regionsubregion- Geographic subregiontimezones- Supported timezones (JSON)latitude- Geographic latitudelongitude- Geographic longitude
State Table
id- Primary keyname- State/province namecountry_id- Foreign key to countrycountry_code- Country codecountry_name- Country namestate_code- State/province codetype- Administrative typelatitude- Geographic latitudelongitude- Geographic longitude
City Table
id- Primary keyname- City namestate_id- Foreign key to statestate_code- State codestate_name- State namecountry_id- Foreign key to countrycountry_code- Country codecountry_name- Country namelatitude- Geographic latitudelongitude- Geographic longitudewikiDataId- WikiData identifier
License
Please refer to the license file for more information.