英文:
Delete all empty folders in Pimcore 10.5 Data Object via PHP
问题
我有一个PHP命令,它从外部来源读取数据并将数据插入到Pimcore 10.5作为数据对象。
这个过程会留下一些空的Pimcore数据对象文件夹。
是否有内置的Pimcore过程可以以编程方式删除所有空文件夹,还是我应该自己遍历树并删除它们?
英文:
I've a PHP command which reads data from an external source and inserts data in Pimcore 10.5 as Data Objects.
This procedure leaves behind a few empty Pimcore Data Object folder.
Is there a built-in Pimcore procedure to remove all empty folders programmatically or should I just traverse the tree and remove them on my own?
答案1
得分: 0
protected function removeEmptyFolders() : static
{
$this->fxTitle("删除空文件夹...");
$oFolders = Folder::getList([
'condition' => "o_type = 'folder' AND o_parentId > 0"
])->load();
$folderNum = count($oFolders);
if( $folderNum == 0 ) {
$this->fxWarning("未检测到文件夹!");
return $this;
}
$this->fxInfo("加载 ##$folderNum## 个文件夹");
$arrFolderByPath = [];
foreach($oFolders as $folder) {
$path = $folder->getPath() . $folder->getKey() . "/";
$arrFolderByPath[$path] = $folder;
}
//
$oDataObjects = Folder::getList([
'condition' => "o_parentId > 0"
])->load();
foreach($oDataObjects as $folder) {
$path = $folder->getPath();
if( array_key_exists($path, $arrFolderByPath) && $folder->getId() != $arrFolderByPath[$path]->getId() ) {
unset($arrFolderByPath[$path]);
}
}
$folderNum = count($arrFolderByPath);
if( $folderNum == 0 ) {
$this->fxInfo("未检测到空文件夹");
return $this;
}
$this->fxInfo("删除 ##$folderNum## 个文件夹...");
foreach($arrFolderByPath as $folder) {
$deletedFolderTitle = implode(' | ', [$folder->getId(), $folder->getPath(), $folder->getKey()]) . PHP_EOL;
if( $this->isNotDryRun() ) {
$folder->delete();
}
$this->fxOK($deletedFolderTitle);
}
if( $this->isDryRun() ) {
// 防止无限递归
return $this;
}
return $this->removeEmptyFolders();
}
英文:
I wrote my own code:
protected function removeEmptyFolders() : static
{
$this->fxTitle("Removing empty folder(s)...");
$oFolders = Folder::getList([
'condition' => "o_type = 'folder' AND o_parentId > 0"
])->load();
$folderNum = count($oFolders);
if( $folderNum == 0 ) {
$this->fxWarning("No folders detected!");
return $this;
}
$this->fxInfo("##$folderNum## folder(s) loaded");
$arrFolderByPath = [];
foreach($oFolders as $folder) {
$path = $folder->getPath() . $folder->getKey() . "/";
$arrFolderByPath[$path] = $folder;
}
//
$oDataObjects = Folder::getList([
'condition' => "o_parentId > 0"
])->load();
foreach($oDataObjects as $folder) {
$path = $folder->getPath();
if( array_key_exists($path, $arrFolderByPath) && $folder->getId() != $arrFolderByPath[$path]->getId() ) {
unset($arrFolderByPath[$path]);
}
}
$folderNum = count($arrFolderByPath);
if( $folderNum == 0 ) {
$this->fxInfo("No empty folders detected");
return $this;
}
$this->fxInfo("Deleting ##$folderNum## folder(s)...");
foreach($arrFolderByPath as $folder) {
$deletedFolderTitle = implode(' | ', [$folder->getId(), $folder->getPath(), $folder->getKey()]) . PHP_EOL;
if( $this->isNotDryRun() ) {
$folder->delete();
}
$this->fxOK($deletedFolderTitle);
}
if( $this->isDryRun() ) {
// prevent infinite recursion
return $this;
}
return $this->removeEmptyFolders();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论