62 lines
2.1 KiB
PHP

<?php
use think\migration\Migrator;
use think\migration\db\Column;
use think\Db;
class Level extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('level', ['comment' => '代理等级', 'engine' => 'InnoDB', 'encoding' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci']);
$table
->addColumn('name', 'string', ['limit' => 20, 'default' => '', 'comment' => '名称'])
->addColumn('status', 'boolean', ['limit' => 1, 'default' => 1, 'comment' => '是否启用'])
->addColumn('create_time', 'integer', ['limit' => 10, 'default' => 0, 'comment' => '创建时间'])
->addColumn('update_time', 'integer', ['limit' => 10, 'default' => 0, 'comment' => '更新时间'])
->addColumn('delete_time', 'integer', ['limit' => 10, 'default' => 0, 'comment' => '删除时间'])
->create();
$this->insertData();
}
protected function insertData()
{
$data = '[{"id":1,"name":"一级代理","status":1},{"id":2,"name":"二级代理","status":1},{"id":3,"name":"三级代理","status":1}]';
$msg = '代理等级导入成功.' . "\n";
Db::startTrans();
$data = json_decode($data, true);
try {
foreach ($data as $item) {
\app\admin\model\Level::create($item);
}
Db::commit();
} catch (\Exception $e) {
Db::rollback();
$msg = $e->getMessage();
}
print ($msg);
}
}