Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

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

Getting URL from Sitetree


Reply

3 Posts   1165 Views

Avatar
njprrogers

29 April 2011 at 5:28am (Last edited: 29 April 2011 5:31am), Community Member, 23 Posts

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

Avatar
njprrogers

29 April 2011 at 10:19pm Community Member, 23 Posts

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

Avatar
Willr

30 April 2011 at 1:34am Forum Moderator, 5511 Posts

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