private function _buildTree(array &$tree, array $parents, array $orderedCategories, CmsCategoryRecord $parentCategory = null)
    {
        //uzupełnienie rodziców
        if ($parentCategory !== null) {
            $parents[$parentCategory->id] = $parentCategory;
        } else {
            $parentCategory = new CmsCategoryRecord;
            $parentCategory->id = null;
        }
        /* @var $categoryRecord CmsCategoryRecord */
        foreach ($orderedCategories as $key => $categoryRecord) {
            //niezgodny rodzic
            if ($categoryRecord->parentId != $parentCategory->id) {
                continue;
            }
            //dodawanie do płaskiej listy
            $this->_flatCategories[$categoryRecord->id] = $categoryRecord;
            //usuwanie wykorzystanego rekordu kategorii
            unset($orderedCategories[$key]);
            //zapis do drzewa
            $tree[$categoryRecord->id] = [];
            $tree[$categoryRecord->id]['record'] = $categoryRecord->setOption('parents', $parents);
            $tree[$categoryRecord->id]['children'] = [];
            //zejście rekurencyjne do dzieci
            $this->_buildTree($tree[$categoryRecord->id]['children'], $parents, $orderedCategories, $categoryRecord);
        }
    }