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.

Data Model Questions /

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

DataObjectSet and getViewer


Go to End
Reply


3 Posts   2236 Views

Avatar
wildflower1975

Community Member, 59 Posts

30 March 2010 at 11:29am

I'm trying to populate a DataObjectSet and then display the values in a template, but I'm running out of sanity.

the code below looks to me just like the code in tutorial 3 where I'm pushing ArrayData into the doSet, but I always end up with a getViewer User error.

If I try returning $doSet->toArray(); then my <% if %> template is shown but without any values, or the ID displayed is the Page ID or the control loop doesn't show.

function ProcessDoSet(){		
		$doSet = new DataObjectSet();		
		if(Director::URLParam('Action') == "ProcessDoSet"){ 
			$ID = Convert::raw2xml(Director::URLParam('ID'));
				if(is_numeric($ID )){					
 					$record = array('Action' => 'stations', 'ApplicationID' => $ID);
 					$doSet->push(new ArrayData($record)); 					
 					$record = array('Action' => 'Anotherstation', 'ApplicationID' => '7');
 					$doSet->push(new ArrayData($record));  										
			}		
			return $doSet;	
		}					
	} 

<% if ProcessDoSet %>
			<p>Processing DoSet Params</p>	
			<% control ProcessDoSet %>
			<p>Action is $Action</p>	
			<p>ApplicationID is $ApplicationID</p>
			<p>ID is $ID</p>
			<% end_control %>
			<% end_if %>

Another thing I've noticed is that the ProcessDoSet function gets run twice (if I put an echo where I'm setting $record) which doesn't sound right.

thanks

Avatar
wildflower1975

Community Member, 59 Posts

30 March 2010 at 2:33pm

Thanks for replying Luke, maybe I'm expecting something too funky but it appears simple enough.

Yes I'm trying to call this function via a URL but was trying to display it in the calling page's template and therefore hide it if I hadn't called it using the <% if %> and <% control %> statements - but it doesn't really work like that and I need to go Ajax (I think)

I've gone back a step and am just trying to echo out a made up DataObjectSet in a template:

function ProcessDoSet(){		
		$doSet = new DataObjectSet();	
				
	$record = array('Action' => 9,'ApplicationID' => 8);
 	$doSet->push(new ArrayData($record)); 					
 	$record = array('Action' => 6,'ApplicationID' => 7);
 	$doSet->push(new ArrayData($record));  
 					
$myArray = array( 'ProcessedData' => array( 
 array('Action' => 'abc', 'ID' => 123), 
 array('Action' => 'def', 'ID' => 456), ) );													
		
		return $this->customise($myArray)->renderWith('TestController2','Page');
		return $this->customise($doSet)->renderWith('TestController2','Page');
	}
	

Should I be able to use <% control %> statements to loop through the ProcessedData like?
in mysite/templates/TestController2.ss

	<p>Processing Params</p>
			<% control ProcessedData %>
			<p>ApplicationID is $ApplicationID</p>
			<p>ID is $ID</p>
			<p>Action is $Action</p>		
		<% end_control %>/code]

I'm using 2.4 and 2.3.7 (on different machines) for testing

Avatar
wildflower1975

Community Member, 59 Posts

31 March 2010 at 2:28pm

Legend Luke :)

It works on 2.3.7 as well.

I guess the tricky bit I couldn't figure out was the extra wrapping of the $doSet, I thought it would be 'complete' for rendering

$data = array('ProcessedData' => $doSet); 

Worthy of a chapter in the 2nd Silverstripe book I reckon