【问题标题】:Getting the parent pages of Page DataObject in SilverStripe在 SilverStripe 中获取 Page DataObject 的父页面
【发布时间】:2019-05-26 14:53:28
【问题描述】:

我正在开发一个 SilverStripe 项目。现在我正在尝试像这样在控制器中通过 ID 获取 Page 对象。

$currentPage = Page::get()->byID($this->ID);

另外,我也想要 $currentPage 的所有父母/父母。但是我在数据库表中找不到 Page 的相关列,例如 parent_page_id 或其他任何内容。如何获取Page数据对象的所有父页面?

【问题讨论】:

    标签: php silverstripe silverstripe-4


    【解决方案1】:

    Page 广义上来说是一个 SiteTree 对象。 ParentID 列存在于 SiteTree 数据库表中。您可以像这样访问页面的父级:

    $parent = $currentPage->Parent();
    

    您也可以使用SiteTree::getParent() 获得相同的结果:

    $parent = $currentPage->getParent();
    

    如果您需要递归获取所有父母,您可以循环执行此操作,直到 ->Parent() 返回 falsy。

    另外,由于SiteTree 使用Hierarchy 作为父/子逻辑,您也可以使用getAncestors() 获取所有父母和祖父母。见the API documentation

    【讨论】:

    • 由于 SiteTree 使用 Hierarchy 作为 Parent/Children 逻辑,您也可以使用 getAncestors() 获取所有父母和祖父母。见api docs
    • @wmk 谢谢,我已将答案转换为社区 wiki 并添加了您的建议
    猜你喜欢
    • 2015-12-23
    • 2013-07-16
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    相关资源
    最近更新 更多