PHPackages                             ruskid/yii2-csv-importer - 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. [PDF &amp; Document Generation](/categories/documents)
4. /
5. ruskid/yii2-csv-importer

ActiveYii2-extension[PDF &amp; Document Generation](/categories/documents)

ruskid/yii2-csv-importer
========================

Helper to import csv files into database

v1.0.0(8y ago)37367.0k↓64.2%15[4 issues](https://github.com/ruskid/yii2-csv-importer/issues)4GPL-2.0PHP

Since Apr 27Pushed 7y ago11 watchersCompare

[ Source](https://github.com/ruskid/yii2-csv-importer)[ Packagist](https://packagist.org/packages/ruskid/yii2-csv-importer)[ RSS](/packages/ruskid-yii2-csv-importer/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (4)

Yii2 CSV Importer to Database
=============================

[](#yii2-csv-importer-to-database)

Helper for CSV imports to tables.

Installation
------------

[](#installation)

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

Either run

```
php composer.phar require --prefer-dist ruskid/yii2-csv-importer "dev-master"

```

or add

```
"ruskid/yii2-csv-importer": "dev-master"

```

to the require section of your `composer.json` file.

Usage
-----

[](#usage)

```
$importer = new CSVImporter;

//Will read CSV file
$importer->setData(new CSVReader([
    'filename' => $this->file->tempName,
    'fgetcsvOptions' => [
        'delimiter' => ';'
    ]
]));

//Import multiple (Fast but not reliable). Will return number of inserted rows
$numberRowsAffected = $importer->import(new MultipleImportStrategy([
    'tableName' => VendorSwType::tableName(),
    'configs' => [
        [
            'attribute' => 'name',
            'value' => function($line) {
                return $line[1];
            },
            'unique' => true, //Will filter and import unique values only. can by applied for 1+ attributes
        ]
    ],
]));

//Import Active Records (Slow, but more reliable). Will return array of primary keys
$primaryKeys = $importer->import(new ARImportStrategy([
    'className' => BusinessType::className(),
    'configs' => [
        [
            'attribute' => 'name',
            'value' => function($line) {
                return $line[2];
            },
        ],
        [
            'attribute' => 'items',
            'virtual' => true, //set non AR DB attribute (behavior or public model attribute)
            'value' => function($line) {
                return Item::find()->select(['id'])->column('id');
            },
        ]
    ],
]));

// More advanced example. You can use queries to set related data.
// Use query caching for performance
$importer->import(new MultipleImportStrategy([
    'tableName' => ProductInventory::tableName(),
    'configs' => [
        [
            'attribute' => 'product_name',
            'value' => function($line) {
                //You cand perform your filters and excludes here. Empty exclude example:
                return $line[7] != "" AppHelper::importStringFromCSV($line[7]) : null;
            },
        ],
        [
            'attribute' => 'id_vendor_sw_type',
            'value' => function($line) {
                $name = AppHelper::importStringFromCSV($line[1]);
                $vendor = VendorSwType::getDb()->cache(function ($db) use($name) {
                    return VendorSwType::find()->where(['name' => $name])->one();
                });
                return isset($vendor) ? $vendor->id : null;
            },
        ],
    ],
]));

//Special case only available with Active Record Strategy.
//Get primary key list of new imported items for later use.
$primaryKeys = $importer->import(new ARImportStrategy([
    'className' => Fabrica::className(),
    'configs' => [
        [
            'attribute' => 'name',
            'value' => function($line) {
                return $line[0];
            },
        ]
    ],
]));

//You can use the primary key list for the next import of related data.
//The order of primary key items will be the same as in csv file.
$importer->import(new MultipleImportStrategy([
    'tableName' => Product::tableName(),
    'configs' => [
        [
            'attribute' => 'id_fabrica',
            'value' => function($line) use (&$primaryKeys) {
                return array_shift($primaryKeys);
            },
            'unique' => true,
        ],
    ],
]));

//You can skip CSV Row / Active Record import if some csv row doesn't meet the requirements.
$importer->import(new MultipleImportStrategy([
    'tableName' => ProductCategory::tableName(),
    'configs' => [
        [
            'attribute' => 'name',
            'value' => function($line) {
                return $line[0];
            },
        ],
        [
            'attribute' => 'description',
            'value' => function($line) {
                return $line[1];
            },
        ],
        [
            'attribute' => 'estado',
            'value' => function($line) {
                return $line[2];
            },
        ]
    ],
    //All ACTIVE categories that don't have name set to 'MS_SQLSERVER' or empty will be imported.
    'skipImport' => function($line){
        if($line[0] == 'MS_SQLSERVER' || $line[0] == ""){
            return true;
        }
        if($line[2] == 'NOT ACTIVE'){
            return true;
        }
    }
]));

//Import or update multiple (Fast but not reliable). Will return number of inserted, updated and unchanged rows
//ARUpdateStrategy can be used instead of MultipleUpdateStrategy if you want to use AR validation.
//The returned value will be the number of inserted, updated and unchanged rows in both cases.
$records = $importer->import(new MultipleUpdateStrategy([
	'className' => Customer::className(),
	'csvKey' => function ($line) {
		return $line[0];
	},
	'rowKey' => function ($row) {
		return $row['gecom_id'];
	},
	'skipImport' => function ($line) {
		return !$line[0];
	},
	'configs' => [
		[
			'attribute' => 'customer_id',
			'value' => function($line) {
				return $line[0];
			},
		],
	],
]));
```

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity47

Moderate usage in the ecosystem

Community22

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 90% 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

Unknown

Total

1

Last Release

2990d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8279841?v=4)[Victor Demin](/maintainers/ruskid)[@ruskid](https://github.com/ruskid)

---

Top Contributors

[![ruskid](https://avatars.githubusercontent.com/u/8279841?v=4)](https://github.com/ruskid "ruskid (36 commits)")[![stupidusername](https://avatars.githubusercontent.com/u/3828886?v=4)](https://github.com/stupidusername "stupidusername (4 commits)")

---

Tags

excelcsvyii2importer

### Embed Badge

![Health badge](/badges/ruskid-yii2-csv-importer/health.svg)

```
[![Health](https://phpackages.com/badges/ruskid-yii2-csv-importer/health.svg)](https://phpackages.com/packages/ruskid-yii2-csv-importer)
```

###  Alternatives

[dracoblue/craur

A lossless xml to json and json to xml converter (and csv/xlsx/yaml). Writing PHP Json/Xml/Csv/Yaml/excel Importers made easy

4643.9k3](/packages/dracoblue-craur)[arogachev/yii2-excel

ActiveRecord import and export based on PHPExcel for Yii 2 framework

6381.4k1](/packages/arogachev-yii2-excel)[phpnt/yii2-export

Yii2 It saves data in xls, csv, word, html, pdf files.

158.9k](/packages/phpnt-yii2-export)[custom-it/yii2-excel-report

An extension for generate excel file from GridView content

371.9k](/packages/custom-it-yii2-excel-report)

PHPackages © 2026

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