Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataObjectSet and getViewer

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

Page: 1
Go to End
Author Topic: 2037 Views
  • wildflower1975
    Avatar
    Community Member
    59 Posts

    DataObjectSet and getViewer Link to this post

    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

  • wildflower1975
    Avatar
    Community Member
    59 Posts

    Re: DataObjectSet and getViewer Link to this post

    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

  • wildflower1975
    Avatar
    Community Member
    59 Posts

    Re: DataObjectSet and getViewer Link to this post

    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

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