Jump to:

22999 Posts in 11854 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Passing an array

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: 508 Views
  • Stef87
    Avatar
    Community Member
    66 Posts

    Passing an array Link to this post

    SilverStripe 2.4.7

    Hi

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

    Basically I was following this approach 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());

             Debug::Show($output);

             $QR = $output;

             $this->QRData = $QR;

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

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

  • therussdotcom
    Avatar
    Community Member
    2 Posts

    Re: Passing an array Link to this post

    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
    Russ

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Passing an array Link to this post

    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

  • Stef87
    Avatar
    Community Member
    66 Posts

    Re: Passing an array Link to this post

    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?

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Passing an array Link to this post

    >> 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): http://doc.silverstripe.org/framework/en/reference/templates

    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.

    Martine

  • Stef87
    Avatar
    Community Member
    66 Posts

    Re: Passing an array Link to this post

    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?

  • martimiz
    Avatar
    Forum Moderator
    1038 Posts

    Re: Passing an array Link to this post

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

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