一 面板执行命令
php artisan XX
比如 php artisan command:batch_register_shop
<?php
namespace App\Console\Commands;
use App\Libraries\Error;
use App\Libraries\Mysql;
use App\Libraries\Shop;
use App\Models\WholesaleCmSupplierTemp;
use App\Services\Goods\CopyGoodsService;
use Illuminate\Console\Command;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use App\Models\MerchantsCategory;
use App\Models\Goods;
use App\Repositories\Common\BaseRepository;
class BatchRegisterShop extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:batch_register_shop';
/**
* The console command description.
*
* @var string
*/
protected $description = '批量( 复制商品信息等)';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
echo '开始:' . date('Y-m-d H:i:s');
$cm_srv = new CopyGoodsService();
Goods::where('user_id', 179)->whereNotIn('goods_id',$arlist)->chunkById(10, function ($goods) use ($cm_srv) {
foreach ($goods as $good) {
$good = $good->toArray();
$res = $cm_srv->importData($good['goods_id'],60);
}
});
echo '结束:' . date('Y-m-d H:i:s');
return ;
}
}
二 分块处理数据
Goods::where(
w
h
e
r
e
)
−
>
c
h
u
n
k
B
y
I
d
(
10
,
f
u
n
c
t
i
o
n
(
where)->chunkById(10, function (
where)−>chunkById(10,function(goods) use (KaTeX parse error: Expected '}', got 'EOF' at end of input: … foreach (goods as $good) {
$good = $good->toArray();
$res =
c
m
s
r
v
−
>
i
m
p
o
r
t
D
a
t
a
(
cm_srv->importData(
cmsrv−>importData(good[‘goods_id’],60);
}
});
分块每次处理10条 整体处理完两千多商品的数据导入 大概用时 18分钟