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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

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

Passing an array


Go to End


7 Posts   1046 Views

Avatar
Stef87

Community Member, 66 Posts

26 October 2012 at 12:43am

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;
	}

Avatar
therussdotcom

Community Member, 13 Posts

26 October 2012 at 8:02am

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

Avatar
martimiz

Forum Moderator, 1391 Posts

26 October 2012 at 8:13am

Edited: 26/10/2012 8:15am

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 :)

Avatar
Stef87

Community Member, 66 Posts

26 October 2012 at 8:36pm

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?

Avatar
martimiz

Forum Moderator, 1391 Posts

27 October 2012 at 4:54am

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

Avatar
Stef87

Community Member, 66 Posts

27 October 2012 at 6:41am

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?

Avatar
martimiz

Forum Moderator, 1391 Posts

28 October 2012 at 7:02am

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