其他分享
首页 > 其他分享> > 数据的迁移

数据的迁移

作者:互联网

TP5下载:

composer require topthink/think-migration=1.0.*   

2.创建迁移文件
php think migrate:create Users


3.编辑文件


public function change()
{
// create the table
$table = $this->table('users',array('engine'=>'InnoDB','comment' => '用户表'));
$table->addColumn('username', 'string',array('limit' => 32,'default'=>'','comment'=>'用户名,登陆使用'))
->addColumn('password', 'string',array('limit' => 32,'default'=>md5('123456'),'comment'=>'用户密码'))
->addColumn('name', 'string',array('limit' => 32,'default'=>'','comment'=>'真实姓名'))
->addColumn('phonenum', 'integer',array('limit' => 32,'default'=>110,'comment'=>'手机号'))
->addColumn('email', 'string',array('limit' => 32,'default'=>'122843744@qq.com','comment'=>'邮箱'))
->addColumn('role_id', 'string',array('limit' => 32,'default'=>'122843744@qq.com','comment'=>'角色'))
->addColumn('login_status', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'登陆状态'))
->addColumn('login_code', 'string',array('limit' => 32,'default'=>0,'comment'=>'排他性登陆标识'))
->addColumn('last_login_ip', 'integer',array('limit' => 11,'default'=>0,'comment'=>'最后登录IP'))
->addColumn('last_login_time', 'datetime',array('default'=>0,'comment'=>'最后登录时间'))
->addColumn('is_delete', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'删除状态,1已删除'))
->addIndex(array('username'), array('unique' => true))
->addTimestamps()
->create();
}
public function up()
{
// add column type DOUBLE
$this->execute("ALTER TABLE third ADD COLUMN much_money DOUBLE(10,2) unsigned NOT NULL DEFAULT '0.00'");
}

public function down()
{
$this->dropTable('users');
}
4.执行文件迁移
php think migrate:run
5.执行回滚
php think migrate:rollback -t third
二.thinkphp 数据填充
1.创建填充
php think seed:create Third
2.编辑填充文件
public function run()
{
$data[] = [
'username' => $faker->userName,
'name' => $faker->name,
'phonenum' => $faker->phoneNumber,
'email' => $faker->email,
'role_id' => 3
];


}
3.开始进行数据库填充
a. php think seed:run

b.可指定一个或多个 seed

c. $ php think seed:run -s Third
$ php think seed:run -s Third -s member

4.安装faker类库
composer require fzaninotto/faker
public function run()
{
$faker = Factory::create('zh_CN');//选择中文库
$data = [];
for ($i = 0; $i < 100; $i++) {
$data[] = [
'username' => $faker->userName,
'name' => $faker->name,
'phonenum' => $faker->phoneNumber,
'email' => $faker->email,
'role_id' => 3
];
}
$this->table('users')->insert($data)->save();

  
 

标签:comment,default,array,faker,limit,addColumn,数据,迁移
来源: https://www.cnblogs.com/qsn17501005/p/15585581.html