PHPackages                             mhunesi/yii2-csc - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mhunesi/yii2-csc

ActiveYii2-extension[Database &amp; ORM](/categories/database)

mhunesi/yii2-csc
================

Yii2 Country State City migration and models.

v1.0.2(3y ago)0231MITPHP

Since Dec 2Pushed 3w ago1 watchersCompare

[ Source](https://github.com/mhunesi/yii2-csc)[ Packagist](https://packagist.org/packages/mhunesi/yii2-csc)[ RSS](/packages/mhunesi-yii2-csc/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

Yii2 Country State City
=======================

[](#yii2-country-state-city)

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
-----------

[](#data-source)

The geographical data used in this extension is sourced from the [Countries States Cities Database](https://github.com/dr5hn/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
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run:

```
composer require --prefer-dist mhunesi/yii2-csc "*"
```

or add to the `require` section of your `composer.json` file:

```
"mhunesi/yii2-csc": "*"
```

Usage
-----

[](#usage)

### Configuration

[](#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

[](#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 relationships
- `city` - Cities with state and country relationships

Models
------

[](#models)

The extension provides three Active Record models:

- `mhunesi\csc\models\Country`
- `mhunesi\csc\models\State`
- `mhunesi\csc\models\City`

### Usage Examples

[](#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
---------------

[](#database-schema)

### Country Table

[](#country-table)

- `id` - Primary key
- `name` - Country name
- `iso2` - ISO2 code (2 characters)
- `iso3` - ISO3 code (3 characters)
- `numeric_code` - Numeric country code
- `phone_code` - International phone code
- `capital` - Capital city
- `currency` - Currency code
- `currency_name` - Currency name
- `currency_symbol` - Currency symbol
- `tld` - Top-level domain
- `native` - Native country name
- `region` - Geographic region
- `subregion` - Geographic subregion
- `timezones` - Supported timezones (JSON)
- `latitude` - Geographic latitude
- `longitude` - Geographic longitude

### State Table

[](#state-table)

- `id` - Primary key
- `name` - State/province name
- `country_id` - Foreign key to country
- `country_code` - Country code
- `country_name` - Country name
- `state_code` - State/province code
- `type` - Administrative type
- `latitude` - Geographic latitude
- `longitude` - Geographic longitude

### City Table

[](#city-table)

- `id` - Primary key
- `name` - City name
- `state_id` - Foreign key to state
- `state_code` - State code
- `state_name` - State name
- `country_id` - Foreign key to country
- `country_code` - Country code
- `country_name` - Country name
- `latitude` - Geographic latitude
- `longitude` - Geographic longitude
- `wikiDataId` - WikiData identifier

License
-------

[](#license)

Please refer to the license file for more information.

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance62

Regular maintenance activity

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

3

Last Release

1299d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15876915?v=4)[Mustafa Hayri ÜNEŞİ](/maintainers/mhunesi)[@mhunesi](https://github.com/mhunesi)

---

Top Contributors

[![mhunesi](https://avatars.githubusercontent.com/u/15876915?v=4)](https://github.com/mhunesi "mhunesi (8 commits)")

---

Tags

stateyii2extensioncountrycity

### Embed Badge

![Health badge](/badges/mhunesi-yii2-csc/health.svg)

```
[![Health](https://phpackages.com/badges/mhunesi-yii2-csc/health.svg)](https://phpackages.com/packages/mhunesi-yii2-csc)
```

###  Alternatives

[dmstr/yii2-migrate-command

Console Migration Command with multiple paths/aliases support

31297.1k6](/packages/dmstr-yii2-migrate-command)[dmstr/yii2-db

Database extensions

19642.9k6](/packages/dmstr-yii2-db)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
