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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Getting URL from Sitetree


Go to End


3 Posts   2124 Views

Avatar
njprrogers

Community Member, 23 Posts

29 April 2011 at 5:28am

Edited: 29/04/2011 5:31am

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

Community Member, 23 Posts

29 April 2011 at 10:19pm

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

Forum Moderator, 5523 Posts

30 April 2011 at 1:34am

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