【发布时间】:2018-11-11 21:36:57
【问题描述】:
使用 laravel-nested 设置 Baum 的包并尝试将树导入 mysql 数据库。导入数据:
1. FirstLevelWord1 - SecondLevelWord1 - ThirdLevelWord1 - FourthLevelWord1
2. FirstLevelWord1 - SecondLevelWord1 - ThirdLevelWord1 - FourthLevelWord2
3. FirstLevelWord1 - SecondLevelWord1 - ThirdLevelWord1 - FourthLevelWord3
我需要更新相同的单词而不是在数据库中写入重复的单词。但是现在我只能用第一级单词来做到这一点 - 不知道如何动态更新第二级和后续级别的相同单词。
导入方式
foreach ($words as $value){
$c = explode("-", $value);
$root = Chain::firstOrCreate(['cWord' => $c[0]]);
for ($i = 1; $i < count($c) ; $i++) {
$prevChild = null;
if (Chain::where('cWord', $c[$i])->first()){
$prevChild = Chain::where('cWord', $c[$i-1])->first();
}
$child = Chain::create(['cWord' => $c[$i]]);
if($i == 1){
$child->makeChildOf($root);
} else {
$child->makeChildOf(
$root->getDescendants()[count($root->getDescendants())-1]);
}
}
$node = Chain::where('cWord', '=', $c[0])->first();
}
取而代之的是这个结构
我明白了
如您所见,第二层和第三层的单词是重复的。有什么想法可以解决这个问题吗?
【问题讨论】:
-
如果您想吸引更多人寻求帮助,您可能需要考虑使用英语价值观
-
是的,忘了这个。固定
标签: php mysql arrays laravel nested-sets