Last active
October 5, 2016 09:05
-
-
Save ydatech/f188a2bebde7229bfdd6d34bb3d4cfbc to your computer and use it in GitHub Desktop.
Install CSV library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Install league/csv | |
| composer require league/csv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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