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

Passing an array


7 Posts   599 Views


26 October 2012 at 12:43am Community Member, 66 Posts

SilverStripe 2.4.7


I've managed to confuse myself utterly so I hope I make sense.

Basically I was following this [url=]approach[/url] and that went fine for the most part.

My problem is that I need to pass the value of $output to the QRData variable so I can output it in a template but since it's an array I'm having trouble. I'm sure it's something obvious but I've spent hours trying to figure it out.

Here's my code

protected $QRData = false;

public function retrieveQR() {

      $var = Director::urlParam('ID');

      if(preg_match('/[0-9]+/', $var)){

         $sqlQuery = new SQLQuery();
         $sqlQuery->select = array(
            'QRCode AS offerCode',
            'QRCodeImage AS QR'
         $sqlQuery->from = array("QRCode");

         $sqlQuery->where = array("OfferID = '" . $var . "'" AND "Status = 0");
         $rawSQL = $sqlQuery->sql();
         $result = $sqlQuery->execute();

         $output = array($result->first());


         $QR = $output;

         $this->QRData = $QR;

         return array();
      else{ return $this->httpError(404, 'Not a valid ID');}

   public function getQRData() {
      return $this->QRData;


26 October 2012 at 8:02am Community Member, 2 Posts

Hi there,

It would be useful to what Debug::show outputs, as well as the output from the getQRData() method. However, at first glance it looks like you're trying to push a standard PHP array into a .ss template which wont work. You need to wrap it in a ViewableData construct first.

Assuming you simply call $QRData or <% control QRData %> in your .ss template, try the following:

public function getQRData() {
return new ArrayData($this->QRData);

Good luck


26 October 2012 at 8:13am (Last edited: 26 October 2012 8:15am), Forum Moderator, 1091 Posts

For use with templates, in ss2.4 you need DataObjects and DataObjectSets. To change your one record into a ViewableData object, do something like:

$row = $result->first();
$this->QRData = new ArrayData($row);

EDIT - I was slow :)


26 October 2012 at 8:36pm Community Member, 66 Posts

Wow thanks. That was it.

So just to clarify, am I correct in thinking that all data used in templates has to be in ViewableData format then? So a DataObject or DataObjectSet? Does this change in SS 3 or does it stay the same?


27 October 2012 at 4:54am Forum Moderator, 1091 Posts

>> So just to clarify, am I correct in thinking that all data used in templates has to be
>> in ViewableData format then? So a DataObject or DataObjectSet?

Noooo, sorry if we gave you that impression, that would make things very hard! You can pass 'single' values like strings, integers, booleans, ...

Have a look at the docs here for SS3.0 (and take the link on top left for versions 2.4):

It's just that if you want to pass a collection of data to the template, like you tried to do with an array, it should be in the form of a DataObject or a DataObjectSet (DataList in SS3.0) instead.



27 October 2012 at 6:41am Community Member, 66 Posts

Thank you so much for your help. I have one more, unrelated question.

How does SilverStripe create and store IDs for images? I have added images in using a mySQL insert into but I need to query them by ID. How would I do this?


28 October 2012 at 7:02am Forum Moderator, 1091 Posts

Silverstripe uses Image objects, a subclass of the File DataObject to store Images, so you should be able to:

$myImage = Image::get()->byID(5);

However, if you do your own MySQL insert queries, you're not using SilverStripe functionality, so I don't know how you've stored them...