Jump to:

3432 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Accessing Parents Parent information

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 4482 Views
  • obj63mc
    Avatar
    Community Member
    24 Posts

    Accessing Parents Parent information Link to this post

    Hi All,

    I am trying to access an objects parent parent. For example. I have a homepage that has a featured article. The article is in a structure of Category >> Sub Cat >> Article. So I can access the parent of the article to get the sub category - $FeaturedArticle.Parent.Link

    When I try $FeaturedArticle.Parent.Parent.Link though this will then simply return an error stating the forTemplate method is not available for category to render. Because of this I added the forTemplate method but then nothing returns at all when I call it. No errors, nothing. My forTemplate function is below, any idea when I then call $FeaturedArticle.Parent.Parent nothing displays on my .ss template? Also does anyone know a better way to access this data from the $FeaturedArticle variable that I have.

    function forTemplate() {
          $whereStatement = "CategoryPage_Live.ID = " . $this->ID; // Select the current category
    $orderBy = null;
    $limit = null;
    $join = null;

    //$whereStatement = "ParentID = ".$this->ParentID." AND ID <> ".$this->ID;
    $cat = DataObject::get("CategoryPage", $whereStatement, $orderBy, $join, $limit);
          
          $string = '<a href="'.$cat->Link.'">'.$cat->Title.'</a>';
          return $string;
       }

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Accessing Parents Parent information Link to this post

    Not sure the template parser supports that level of nested commands (Parent.Parent). You probably have to wrap it in a control

    <% control FeaturedArticle %>
    <% control Parent %>
    $Parent.Link
    <% end_control %>
    <% end_control %>

  • wee-man
    Avatar
    Community Member
    21 Posts

    Re: Accessing Parents Parent information Link to this post

    I think Willr is right.
    But it makes the templates "ugly" ;)

    If i have such a situation i use delegate-methods to avoid control..control..control blocks in the templates.
    For example FeaturedArticle

    function Category() {
    return $this->Parent()->Parent();
    }

    Now the template looks like this

    $Category.Link

  • obj63mc
    Avatar
    Community Member
    24 Posts

    Re: Accessing Parents Parent information Link to this post

    Thanks all, these solutions work perfectly.

    4482 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.