删除Pimcore 10.5数据对象中的所有空文件夹,通过PHP。

huangapple go评论106阅读模式
英文:

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();
    }

huangapple
  • 本文由 发表于 2023年6月5日 18:54:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定