新增导入树状数据方法

This commit is contained in:
JaguarJack 2020-04-29 15:05:49 +08:00
parent 76855d6b5d
commit 514bb6b520
5 changed files with 70 additions and 1 deletions

View File

@ -0,0 +1 @@
<?php

View File

@ -1,6 +1,7 @@
<?php <?php
namespace catcher; namespace catcher;
use think\facade\Db;
use think\helper\Str; use think\helper\Str;
class Utils class Utils
@ -50,4 +51,40 @@ class Utils
return array_merge($search, $params); return array_merge($search, $params);
} }
/**
* 导入树形数据
*
* @time 2020年04月29日
* @param $data
* @param $table
* @param string $pid
* @param string $primaryKey
* @return void
*/
public static function importTreeData($data, $table, $pid = 'parent_id',$primaryKey = 'id')
{
$table = \config('database.connections.mysql.prefix') . $table;
foreach ($data as $value) {
if (isset($value[$primaryKey])) {
unset($value[$primaryKey]);
}
$children = $value['children'] ?? false;
if($children) {
unset($value['children']);
}
$id = Db::name($table)->insertGetId($value);
if ($children) {
foreach ($children as &$v) {
$v[$pid] = $id;
$v['level'] = !$value[$pid] ? $id : $value['level'] . '-' .$id;
}
self::importTreeData($children, $table, $primaryKey);
}
}
}
} }

View File

@ -4,7 +4,6 @@ declare (strict_types = 1);
namespace catcher\command; namespace catcher\command;
use catcher\CatchAdmin; use catcher\CatchAdmin;
use think\Config;
use think\console\Command; use think\console\Command;
use think\console\Input; use think\console\Input;
use think\console\input\Argument; use think\console\input\Argument;

View File

@ -0,0 +1,32 @@
<?php
declare (strict_types = 1);
namespace catcher\command\Tools;
use catcher\CatchAdmin;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
class BackUpDataCommand extends Command
{
protected $table;
protected function configure()
{
// 指令配置
$this->setName('backup:data')
->addArgument('tables', Argument::REQUIRED, 'backup tables')
->addOption('zip', '-z',Option::VALUE_NONE, 'is need zip')
->setDescription('backup data you need');
}
protected function execute(Input $input, Output $output)
{
$output->info('succeed!');
}
}