3062 Posts in 864 Topics by 646 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1695 Views |
-
DataObjectSet and getViewer

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
-
Re: DataObjectSet and getViewer

30 March 2010 at 12:45pm Last edited: 30 March 2010 12:59pm
I'm guessing you're trying to call this function from the URL ?
eg:However you're also trying to call it from the template, with the <% if ProcessDoSet %> and the <% control ProcessDoSet %>.
This is mixing a couple of ideas, if that's what you're doing. I'm not sure quite what you're trying to achieve.
If you're calling it from a template with 'if' and 'control' then you'd return a DataObjectSet and use the 'control' to loop through it as you have done in the template.
If however you're calling it via the URL, using the Action and ID parameters, then you need to return the rendered text directly.
This would typically done by something like this:....
return $this->customise($data)->renderWith( 'ProcessDoSet_results', 'Page' );
// renderWith takes a list of valid template names to be tried in order.
// $data would be either a DataObjectSet, or an associative array, such as:
// array( 'ProcessedData' => array(
// array('Action' => 'abc', 'ID' => 123),
// array('Action' => 'def', 'ID' => 456),
// ));I hope that helps. Perhaps you can tell us what you're trying to achieve in more detail, if not?
See also http://silverstripe.org/data-model-questions/show/281858
Cheers,
- Luke -
Re: DataObjectSet and getViewer

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
-
Re: DataObjectSet and getViewer

31 March 2010 at 10:33am Last edited: 31 March 2010 1:52pm
Well, you can use the following method, which I've tested in a 2.4 instance, but not a 2.3.
Anyway, the following should work.
You access this using http://site.url/pageurl/ProcessDoSet/123
Page_Controller
function ProcessDoSet() {
$ID = $Action = null;
if (Director::urlParam('ID')) {
$ID = Convert::raw2xml(Director::urlParam('ID'));
}
if (Director::urlParam('Action')) {
$Action = Convert::raw2xml(Director::urlParam('Action'));
}
$doSet = new DataObjectSet();
$doSet->push(new ArrayData(array('Action' => $Action, 'ApplicationID' => $ID)));
$doSet->push(new ArrayData(array('Action' => 6, 'ApplicationID' => 7)));$data = array('ProcessedData' => $doSet);
return $data;
// or you can choose the template with:
//return $this->customise($data)->renderWith('TemplateName');
}Template
<% if ProcessedData %>
<p>Processing Params</p>
<% control ProcessedData %>
<p>ApplicationID is $ApplicationID</p>
<p>ID is $ID</p>
<p>Action is $Action</p>
<% end_control %>
<% end_if %>There is potential confusion here as the 'if' and 'control' will first look for a defined variable in the current template scope, and failing that will look for a method on the controller, with fallback to the Page methods/properties.
So when we had <% if ProcessDoSet %> it was calling a Controller method, but in the case above, the <% control ProcessedData %> is using the array of data which was used to customise the controller before rendering.Cheers,
- Luke -
Re: DataObjectSet and getViewer

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
| 1695 Views | ||
|
Page:
1
|
Go to Top |


