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.

Data Model Questions /

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

Fetching Data from MSSQL Database and Rendering in Template


Go to End


3 Posts   1805 Views

Avatar
alex_sm

Community Member, 19 Posts

11 June 2015 at 12:44am

Edited: 11/06/2015 12:46am

Hello everybody!
I'm newbuy, so please help me.
I'm runing SS 3.1.12 with silverstripe/silverstripe-mssql module. I want to fetch data from MSSQL database and render them on page using template. My code:

class FlotPage extends Page {
  };

class FlotPage_Controller extends Page_Controller {
  public function ShowFlot() {
     $con =  DB::connect([
	'type'=>'MSSQLDatabase',
	'server'=>'MSSQL',
	'username'=>'user',
	'password'=>'12345678',
	'database'=>'database'	
	],'MSSQL connection');
$result =  DB::getConn('MSSQL connection')->query('select top 5 flo_rn,flo_nm,flo_data from flo_table');
   
$records = new ArrayList();
foreach($result as $row) {
  $records->push(new ArrayData($row));
     } 
  return $records;
    }
}

When I open the page in browser a message appears: [User Error] Uncaught Exception: Object->__call(): the method 'fortemplate' does not exist on 'ArrayList'
I've read several posts in blog, they didn't help me.

Avatar
Devlin

Community Member, 344 Posts

11 June 2015 at 1:27am

The template has not got a clue on how to render $ShowFlot. If ArrayList would have a forTemplate() method, it would use that.

Try this instead:

<% loop $ShowFlot %>
$flo_rn
$flo_nm
$flo_data
<% end_loop %>

Avatar
alex_sm

Community Member, 19 Posts

16 June 2015 at 6:23pm

Thank you Delvin! Your advice is very helpful for me. Everything works!