Skip to content

Instantly share code, notes, and snippets.

@ydatech
Last active October 5, 2016 09:05
Show Gist options
  • Select an option

  • Save ydatech/f188a2bebde7229bfdd6d34bb3d4cfbc to your computer and use it in GitHub Desktop.

Select an option

Save ydatech/f188a2bebde7229bfdd6d34bb3d4cfbc to your computer and use it in GitHub Desktop.
Install CSV library
# Install league/csv
composer require league/csv
<?php
use yii\db\Migration;
use League\Csv\Reader;
/**
* Handles the creation for table `provinsi`.
*/
class m160904_132618_create_provinsi_table extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
$this->createTable('provinsi', [
'id' => $this->primaryKey(),
'nama' => $this->string(50),
]);
// path tempat file csv berada
$provinsi = Yii::getAlias('@app/migrations/provinsi.csv');
// baca file csv menggunakan library league\csv
$reader = Reader::createFromPath($provinsi);
// insert data provinsi kedalam tabel provinsi
foreach ($reader as $index => $row) {
$this->insert('provinsi', [
'id' => (int)$row[0],
'nama' => $row[1],
]);
}
}
/**
* @inheritdoc
*/
public function safeDown()
{
// path tempat file csv berada
$provinsi = Yii::getAlias('@app/migrations/provinsi.csv');
// baca file csv menggunakan library league\csv
$reader = Reader::createFromPath($provinsi);
// hapus data provinsi dari tabel provinsi
foreach ($reader as $index => $row) {
$this->delete('provinsi', ['id' => (int)$row[0]]);
}
$this->dropTable('provinsi');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment