本地执行TP5框架的方法

1配置环境变量
本地执行TP5框架的方法_第1张图片
例如我的环境变量 C:\Users\Administrator\AppData\Roaming\Composer\vendor\bin;F:\PhpStudy\PHPTutorial\php\php-7.1.13-nts\php.exe(注意用;隔开)

或者cmd进入;F:\PhpStudy\PHPTutorial\php\php-7.1.13-nts

2再app新建一个command文件夹,新建一个test.php文件,注意看图片
本地执行TP5框架的方法_第2张图片
代码如下
` namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class Test extends Command
{
protected function configure()
{
$this->setName(‘test’);//定义命令的名字
}

protected function execute(Input $input, Output $output)
{
    set_time_limit(0);
    $array_brand = [];
    $brandInfo   = Db::name('StoreCommunityBrands')->field('user_id,id')->select();
    foreach ($brandInfo as $v) {
        $array_brand[$v['id']] = $v['user_id'];
    }
    $userInfo = Db::name('StoreUser')->field('id')->select();
    foreach ($userInfo as $v) {
        echo "\n";
        echo $v['id'];
        $brand_id = $this->getPid($v['id'], $array_brand);
        if($brand_id==null || $brand_id==0){
            $brand_id = 1;
        }
        //写入社群
        Db::name('StoreUser')->where('id',$v['id'])->update(['brand_id'=>$brand_id]);
    }
    echo "<结束>";exit;
}

public function getPid($id, $array_brand)
{
    $result = Db::name('StoreUser')->field('reference')->where('id', $id)->find();
    if ($result){
        if (in_array($id,$array_brand)) {
            $brand_id = array_search($id, $array_brand);
            return $brand_id;
        }elseif(in_array($result['reference'], $array_brand)){
            $brand_id = array_search($result['reference'], $array_brand);
            return $brand_id;
        }
        $this->getPid($result['reference'],$array_brand);
    }else{
        return 1;
    }
}

}
`

//代码可以自己更改

execute()这个方法是执行的页面逻辑

3在command里面加入你的命名控空间本地执行TP5框架的方法_第3张图片

4命令行
本地执行TP5框架的方法_第4张图片
进入到你的项目
然后看你的方法是否已经进入
本地执行TP5框架的方法_第5张图片

之后就可以 用 php think test 命令,执行你的业务逻辑了
在这里插入图片描述

不懂的同学可以加我v 17705921980 咨询(互帮互助)

你可能感兴趣的:(本地执行TP5框架的方法)