一. 迁移工具使用
命令
- 安装
composer require topthink/think-migration
- 创建迁移文件
//执行命令,创建一个操作文件,一定要用大驼峰写法,如下 php think migrate:create AnyClassNameYouWant //执行完成后,会在项目根目录多一个database目录,这里面存放类库操作文件 //文件名类似/database/migrations/20190615151716_any_class_name_you_want.php
- 执行迁移工具
php think migrate:run
实例
- 添加表
public function change() { /*插件应用表*/ $this->table('addon_app') ->addColumn('uid', 'integer',array('limit' => 11,'comment'=>'创建人')) ->addColumn('soft', 'integer',array('limit' => 11,'comment'=>'排序')) ->addColumn(Column::string('desc',50)->setComment('应用的描述')) ->addColumn(Column::smallInteger('status')->setComment('0是未上架1是设计中2是已上架')->setDefault(0)) # setDefault设置默认值 ->addColumn(Column::text('identification',20)->setComment('应用标识 唯一')) ->addColumn(Column::dateTime('create_time')->setComment('添加时间')) ->addColumn(Column::dateTime('update_time')->setComment('修改时间')) ->addColumn(Column::dateTime('delete_time')->setComment('删除时间')->setNullable()) ->create(); }
- 新增字段
$this->table('addon_app')->addColumn(Column::string('name',20)->setComment('应用名'))->save();
- 删除字段
$this->table('user')->removeColumn('name');
- 编辑字段
$this->table('addon_ping_order')->changeColumn(Column::string('versions',100)->setComment('购买时的应用版本'))->save();
二. 命令行使用
- 创建模型命令
多应用 php think make:model index@Blog 单应用 php think make:model Blog
- 快速生成控制器
多应用 php think make:controller index@Blog 单应用 php think make:controller Blog 仅生成空控制器 php think make:controller index@Blog --plain php think make:controller index@Blog --api 生成多级控制器 php think make:controller index@test/Blog --plain
- 快速生成验证器类
php think make:validate index@User
三. 模型
-
删除
/** * 批量软删除 * @param $map * @return bool */ public function deleteDataList($map) { $res = self::destroy(function($query) use ($map){ $query->where($map); }); return $res; } /** * 软删除 * @param $map * @return bool */ public function deleteData($id) { $res = self::destroy($id); return $res; }
-
一对多, 远程一对多关联
/** * 关联版本表 一对多 * @return \think\model\relation\HasMany */ public function ApkVersion() { return $this->hasMany(ApkVersion::class); } /** * 远程一对多 * @return \think\model\relation\HasManyThrough */ public function ApkApply() { return $this->hasManyThrough(ApkApply::class, ApkVersion::class); } /** * 单条数据 * @param $map * @param string $field * @return array|null|\think\Model * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getFindDa($map,$field = "*") { $res = $this->withSearch(['ls_retail_admin_id'],$map) # 搜索器使用 ->field($field) ->find(); $res2 = $res->ApkApply()->count(); # 统计这个应用有多少人申请下载 dd($res2); $res1 = $res->ApkVersion()->where('status',1)->order('create_time desc')->find(); # 查找这个应用符合条件的一个版本 dd($res1); return $res; }
-
相对关联
// 第一种 public function ApkVersion() { return $this->belongsTo(ApkVersion::class); } public function getListData($map,$field = '*') { $res = $this->field($field) ->withSearch(['limit','page','ls_retail_admin_id','apk_version'],$map) ->with(['ApkVersion' => function($query) { $query->field('versions,id'); # 一定要查询ApkVersion的主键 }]) ->order('create_time','desc') ->select(); $res = $res->bindAttr('ApkVersion',['versions']);# 动态绑定关联属性,就是多一个versions这个key, /*隐藏输出*/ $res->hidden(['ApkVersion','logo','versions','apk_version_id']); return $res; } // 第二种 public function ApkApp() { return $this->belongsTo(ApkApp::class); } $modle = app('model/ApkVersion'); $res = $modle::with('ApkApp')->find($value); if (!$res || !$res->ApkApp){ return 'apk应用不存在'; } dd($res->ApkApp->addon_support_app_id)