tp6.基本功能,tp数据库迁移工具,模型关联

一. 迁移工具使用

命令

  1. 安装
    composer require topthink/think-migration
    
  2. 创建迁移文件
    //执行命令,创建一个操作文件,一定要用大驼峰写法,如下
    
    php think migrate:create AnyClassNameYouWant
    
    //执行完成后,会在项目根目录多一个database目录,这里面存放类库操作文件
    
    //文件名类似/database/migrations/20190615151716_any_class_name_you_want.php
    
  3. 执行迁移工具
    php think migrate:run
    

实例

  1. 添加表
    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();
    }
    
  2. 新增字段
     $this->table('addon_app')->addColumn(Column::string('name',20)->setComment('应用名'))->save();
    
  3. 删除字段
    $this->table('user')->removeColumn('name');
    
  4. 编辑字段
    $this->table('addon_ping_order')->changeColumn(Column::string('versions',100)->setComment('购买时的应用版本'))->save();
    

二. 命令行使用

  1. 创建模型命令
    多应用
    php think make:model index@Blog
    单应用
    php think make:model Blog
    
  2. 快速生成控制器
    多应用
    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
    
  3. 快速生成验证器类
    php think make:validate index@User
    

三. 模型

  1. 删除

    /**
     * 批量软删除
     * @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;
    }
    
  2. 一对多, 远程一对多关联

        /**
     * 关联版本表  一对多
     * @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;
    }
    
  3. 相对关联

    // 第一种
    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)
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值