Answer the question
In order to leave comments, you need to log in
Bug when copying a tree?
There is a certain tree structure of folders. Everything is stored in psql. You need to copy this structure so that the folders have new id's, but parent-child relationships remain between all new folders. Because since foreign keys are used in the database, it is necessary to create folders recursively, from root to child, so that all the necessary links are created in the database.
We get all the current folders and call the recursive function:
$groups = Group::query()->execute();
$newGroupId = $this->deployChildGroups($groups, 0, []);
/**
* @param $platformId
* @param $parentId
* @param $sourceGroups Device\Group[]
* @param $resultsId
*/
public function deployChildGroups($sourceGroups, $parentId, $resultsId)
{
// Если исходные папки находятся в корневой директории, то и новые создаем там же
if ($parentId == 0) {
$newParentId = 0;
} else {
if (! isset($resultsId[$parentId])) {
return false;
}
$newParentId = $resultsId[$parentId];
}
foreach ($sourceGroups as $sourceGroup) {
if ($sourceGroup->getParentId() == $parentId) {
$group = new Group();
$group->setName($sourceGroup->getName());
$group->setParentId($newParentId);
$group->save();
$resultsId[$sourceGroup->getId()] = $group->getId();
$resultsId = $this->deployChildGroups($sourceGroups, $sourceGroup->getId(), $resultsId);
}
}
return $resultsId;
}
{"210":"296","273":"297","274":"298"}
Answer the question
In order to leave comments, you need to log in
Haven't looked at the bypass order?
if (! isset($resultsId[$parentId])) {
return false;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question