thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路

使用 composer 下载thinkPhp6
切换阿里云镜像:composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

composer create-project topthink/think tp                       tp6 composer 下载地址
composer create-project topthink/think

1、redirect重新定向 转跳网址

redirect('/admin/index')->send();

2、渲染html

return View::fetch(“Index/index”,[‘result’=> 111,‘strat’=> 222,‘grades’=> 333]);

thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第1张图片

3、阿里云函数计算缓存问题处理

在使用函数计算fun上传thinkphp6之后,需要做些小处理,最关键的是函数计算么有写入的权力,只能读。so我们的日志文件或者说runtime文件里的东西就要换个地方存储。但是问题来了,在配置log和cache的时可以直接更改他们的path文件。但是会在试图配置缓存报错也就是下面这个鬼样子
thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第2张图片
究其原因就是目录的路径是错误的没有写入权限。打印出来是这样子
在这里插入图片描述
这是我们的根目录,所以会报错我们要改成挂载的目录,专门放我们的缓存文件也就应该是下面这个样子 DIRECTORY_SEPARATOR 是分隔符,
在这里插入图片描述
更改的方法就是找到这个路径 \vendor\topthink\framework\src\think下的App.ph文件更改下路径即可
thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第3张图片

注意需要按照linux的写法来

DIRECTORY_SEPARATOR . 'mnt' . DIRECTORY_SEPARATOR . 'thinkphp' . DIRECTORY_SEPARATOR  .  'runtime' . DIRECTORY_SEPARATOR;

4、DB数据库操作加载用--------注意linux区分大小写:

thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第4张图片

Call to undefined method think\Db::table()
//上述错误是因为我们虽然引入了think\Db这个类,但是没有实例化,所以不能使用Db::table()这种操作
解决方案:需要引入think\Facade\Db这个类才能使用Db::table()

thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第5张图片

上传到阿里云函数计算后发现--------本地可以,上面不行,纠结良久,发现是大小写问题,哎。。。。。。。。

thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第6张图片
thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第7张图片

更改即可使用

5、在页面HTML中隐藏或者说注释一个代码有两种方式

  • (1) 在页面上 ctrl+/ 注释。并将需要加载到前端页面的变量使用 // 隐藏。如下
    thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第8张图片
    在查看网页源代码页面里显示如下
    thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第9张图片
  • (2) 在页面上使用 if 语句注释。如下
    thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第10张图片
    在查看网页源代码页面 显示空白。

6、thinkphp6 之 Apache、Nginx 、IIS 伪静态配置更改

7、开启事务处理。只有里面的所有都执行成功后,才会真正执行,否则一个失败就全部停止执行

// 开启事务
        $this->startTrans();
        try {
            // 添加商品
            $this->allowField(true)->save($data);
            // 商品规格
            $this->addGoodsSpec($data);
            // 商品图片
            $this->addGoodsImages($data['images']);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }

在thinkphp6中,原先的基类都变没了。其中返回的error错误也“搬家”了
具体位置如下(难受。。。。。。)

$this->validate->getError();

thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第11张图片

thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第12张图片

8、error和sql日志单独记录

    'apart_level' => ['begin', 'error', 'sql', 'yoshop-info'],

thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第13张图片

9、数据库左链—leftJoin。右链—rightJoin。直观概念

转载于-----《数据库中的左连接和右连接的区别》

	有两个表a,b
    若是使用a表左链b表,会返回a表所有符合查询条件的数据。以及符合条件的b表的部分数据。写法样式如下

thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第14张图片

10、关于with的用法

首先在你需要调用的model上或者其继承的父类model上写入一个新的方法。根据自己的需要看是一对多,还是一对一,以及上下级的关系。去定义方法。举例假设查询订单表中的一个订单,以及多个发货状态。如下即可

thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第15张图片
thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第16张图片
还可以选择对多层关联嵌套的with进行数据控制
thinkphp6基本变化(杂七杂八)---阿里云函数计算采坑之路_第17张图片

  	

namespace app\back\model\sale;
use  app\back\model\AdminUser as AdminUser;
class SaleOrder extends BaseModel {
    /**
     * 查询数据,关联表查姓名
     * @param array $where
     * @return bool
     */
    public function postSelect($where)
    {
        $res= $this->alias('o')
            ->with([
                'SaleOrderSend'=> function($query) {
                $query->field('order_number,send_count,set_baby,send_state,create_time,send_id')
                	->with(['SaleSendCar'=>function($query){
                    	$query->field('send_id,driver,car');
                }]);
            },
                'SalePrice'=> function($query) {
                $query->field('goods_id,price_fixed');
            },
                'AdminUser'=> function($query) {
                $query->field('admin_id,admin_user_name');
            },
                'Goods'=> function($query) {
                $query->field('goods_id,goods_name');
            },
                'GoodsType'=> function($query) {
                $query->field('goods_type_id,goods_type_name');
            },

                ])
            ->where($where)//定价表未删除数据
            ->order('create_time DESC')
            ->select()
            ->toArray();
        return $res;
    }


namespace app\back\model\sale;

use think\Validate;
use app\back\model\BaseModel as Model;

class BaseModel extends Model {

//数据库查询数据
    public function AllSelect($where = "",$field = "",$order = "")
    {
        $res= $this->where($where)
            ->field($field)
            ->order($order)
            ->select()
            ->toArray();
        return $res;
    }
    //数据库插入数据(返回id)
    public function getInsertGetId($data)
    {
        $res = $this->insertGetId($data);
        return $res;
    }
    //数据库插入数据(不返回id)
    public function getInsert($where)
    {
        $res = $this->insert($where);
        return $res;
    }
    //返回错误信息
    public function getError()
    {
        $validate = new Validate();
        $validate->getError();
    }
    //数据库修改数据
    public function getChangeId($where,$data)
    {
        $res= $this->where($where)->update($data);
        return $res;
    }

    //数据库删除数据
    public function getDeleteId($where,$data)
    {
        $res= $this->where($where)->update($data);
        return $res;
    }
    /**
     * 查询该订单下的所有发货信息
     * @param string $data
     * @return bool
     */
    public function SaleOrderSend()
    {
        return $this->hasMany('SaleOrderSend', 'order_number','order_number');
    }
    /**
     * 查询人的相关信息
     * @param string $data
     * @return bool
     */
    public function AdminUser()
    {
        return $this->hasOne('app\back\model\AdminUser', 'admin_id','admin_id');
    }
    /**
     * 获取定价(这边的单价)---(定价表)
     * @param string $data
     * @return bool
     */
    public function SalePrice()
    {
        return $this->hasOne('SalePrice', 'goods_id','goods_id');
    }
    /**
     * 获取物品名称---(物资管理表)
     * @param string $data
     * @return bool
     */
    public function Goods()
    {
        return $this->hasOne('Goods', 'goods_id','goods_id');
    }

    /**
     * 获取物品类型---(物资类型表)
     * @param string $data
     * @return bool
     */
    public function GoodsType()
    {
        return $this->hasOne('GoodsType', 'goods_type_id','goods_type_id');
    }

}

你可能感兴趣的:(thinkphp6)