Jump to:

23484 Posts in 18977 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Getting URL from Sitetree

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 1153 Views
  • njprrogers
    Avatar
    Community Member
    23 Posts

    Getting URL from Sitetree Link to this post

    Hi,

    I'm new to SS so I'd appreciate any assistance!

    I have a SQL Query to retrieve some sitetree records from a dataobject (which is created by a DailyTask).

    The query returns 10 sitetree dataobjects selected through a join with the Top Ten dataobject.

    I loop through those in the template.ss and wish to provide a link in the href to each one.

    I would like to know the most efficient way to retrieve the url link for those dataobjects as $Link does not work (it aint a field on the dataobjectset).

    function GetTopTen ($country) {
    $sqlQuery = new SQLQuery();
             $sqlQuery->select = array('*');
             $sqlQuery->from = array("TopTen",
             "FULL JOIN SiteTree ON TopTenPageID = SiteTree.ID"
             );
             $sqlQuery->where = array("TopTenCountry = 'ie'");
             $sqlQuery->limit = "10";
             // execute and return a Query-object
             $result = $sqlQuery->execute();
             $dataObject1 = new DataObjectSet();

       foreach($result as $row) {
       //move the SQL result data to the DataObjectSet
       $dataObject1->push(new ArrayData($row));
       }
             return $dataObject1;

    My template layer loops through the returned dataobjectset and outputs the Page with a link to it.

    <% control GetTopTen(ie) %>
       <li><a href="<% GetLink($ID) %>">$Title</a></li>
    <% end_control %>

    Thanks in advance!

    Nick

  • njprrogers
    Avatar
    Community Member
    23 Posts

    Re: Getting URL from Sitetree Link to this post

    Please Help! I still can't find an elegant solution to this one and I know there has to be something fairly straightforward to achieve this.....

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Getting URL from Sitetree Link to this post

    <% GetLink($ID) %>

    You can't pass variables into functions (as is mentioned on the template pages) as of the 2.x series. You would do your link function inside the loop you already have in the PHP.

    foreach($result as $row) {
    // get the link
    $row['Link'] = ... your function link($row ['ID']) ....

    //move the SQL result data to the DataObjectSet
    $dataObject1->push(new ArrayData($row));
    }

    Then you would simply use $Link in the template.

    1153 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.