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文件,注意看图片
代码如下
`<?php
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里面加入你的命名控空间
4命令行
进入到你的项目
然后看你的方法是否已经进入
之后就可以 用 php think test 命令,执行你的业务逻辑了
不懂的同学可以加我v 17705921980 咨询(互帮互助)