1
votes

I am developing a SilverStripe project. Now I am trying to get the Page object by ID in the controller like this.

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

Also, I want to all the parents/parent of the $currentPage too. But I cannot find related columns in the database table for Page like parent_page_id or whatsoever. How can I get all the parent pages of Page data object?

1

1 Answers

3
votes

A Page is broadly speaking a SiteTree object. The ParentID column exists on the SiteTree database table. You can access a page's parent like so:

$parent = $currentPage->Parent();

You can also use SiteTree::getParent() for the same result:

$parent = $currentPage->getParent();

If you need to get all parents recursively, you can do that in a loop until ->Parent() returns falsy.

Also, as SiteTree uses Hierarchy for the Parent/Children logic, you can also use getAncestors() to get all parents and grandparents. See the API documentation.