按深度排序Silverstripe SiteTree

时间:2011-10-03 01:22:17

标签: php silverstripe

我希望获取Silverstripe网站的所有SiteTree页面,然后按深度降序对它们进行排序。深度我的意思是他们的父母数量。

这已在某种程度上由Google Sitemaps模块完成。除非它没有超过10的深度并且不计算从搜索中隐藏的页面:https://github.com/silverstripe-labs/silverstripe-googlesitemaps

查看Google Sitemaps模块模块,可以轻松计算网页父母的数量:(/ code/GoogleSitemapDecorator.php-第78行)

$parentStack = $this->owner->parentStack();
$numParents = is_array($parentStack) ? count($parentStack) - 1: 0;

但是使用此计算对SiteTree进行排序的最佳方法是什么?

我希望有一种比获取所有SiteTree更简单的方法,追加深度,然后求助。

2 个答案:

答案 0 :(得分:3)

下面的函数将获取数据库中所有已发布的页面,检查它们是否可见而不是错误页面,找出它们有多少祖先,然后返回按深度排序的页面的DataObjectSet(祖先数)。 / p>

public function CustomPages() {
    $filter = '';
    $pages = Versioned::get_by_stage('SiteTree', 'Live', $filter);
    $custom_pages = new DataObjectSet();
    if($pages) {
        foreach($pages as $page) {
            if( $page->canView() && !($page instanceof ErrorPage) ) {
                $parentStack = $page->parentStack();
                $numParents = is_array($parentStack) ? count($parentStack) - 1 : 0;
                $page->Depth = $numParents;
                $custom_pages->push($page);
            }
        }
    }
    $custom_pages->sort('Depth', 'DESC');
    return $custom_pages;
}

答案 1 :(得分:0)

如果您正在使用PostgreSQL,您还可以使用单个数据库查询。为了加速SilverStripe(2.4),我们用单个数据库查询替换了getSiteTreeFor(),请参阅:

http://fvue.nl/wiki/SilverStripe:_Replace_getSiteTreeFor_with_single_query

breadcrumb字段包含可以对其进行排序的ID数组。

相关问题