新增导入树状数据方法
This commit is contained in:
parent
76855d6b5d
commit
514bb6b520
1
catch/system/database/seeds/GenerateSeed.php
Normal file
1
catch/system/database/seeds/GenerateSeed.php
Normal file
@ -0,0 +1 @@
|
||||
<?php
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace catcher;
|
||||
|
||||
use think\facade\Db;
|
||||
use think\helper\Str;
|
||||
|
||||
class Utils
|
||||
@ -50,4 +51,40 @@ class Utils
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ declare (strict_types = 1);
|
||||
namespace catcher\command;
|
||||
|
||||
use catcher\CatchAdmin;
|
||||
use think\Config;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Argument;
|
32
extend/catcher/command/Tools/ExportDataCommand.php
Normal file
32
extend/catcher/command/Tools/ExportDataCommand.php
Normal 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!');
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user