Jump to:

22976 Posts in 11667 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » SS3 - custom query to viewable data throwing error but nothing in logs

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: 421 Views
  • obj63mc
    Avatar
    Community Member
    24 Posts

    SS3 - custom query to viewable data throwing error but nothing in logs Link to this post

    Hi All,

    I am trying to display some specific data in a template and I couldn't get the new ORM to query the data with out erroring out, bug report has been sent on that but when using a custom query it still errors out when converting to some form of ViewableData such as ArrayList or ArrayData. No matter what I do it won't work.

    Code is below - any ideas on why this won't work.

    public function GetStates() {
          $results = DB::query('SELECT s.ID AS ID, s.Name AS Name, s.HasSpecial AS HasSpecial FROM State AS s JOIN Program AS P ON P.StateID = s.ID GROUP BY s.ID ORDER BY s.Name ASC;');
          $states = array();
          for ($i = 0; $i < $results->numRecords(); $i++) {
       $record = $results->nextRecord();
             $states[] = $record;
          }
          
          $data = new ArrayData($states);
          return $data;
       }

    template has -

    <% control GetStates %>
    <option value="$ID" data-special="$HasSpecial">$Name</option>
    <% end_control %>

  • obj63mc
    Avatar
    Community Member
    24 Posts

    Re: SS3 - custom query to viewable data throwing error but nothing in logs Link to this post

    Okay I was able to solve this but you have to do something that didn't seem standard or at least when viewing the api docs it doesn't make sense. The api docs state you can simply pass in an array or object to create an ArrayList but really it has to be an array of ArrayData objects... not really clear the way they have it documented...

    In the end I was able to do the following -

    public function GetStates() {
          $results = DB::query('SELECT s.ID AS ID, s.Name AS Name, s.HasSpecial AS HasSpecial FROM State AS s JOIN Program AS P ON P.StateID = s.ID GROUP BY s.ID ORDER BY s.Name ASC;');
          $states = ArrayList::create();
          for ($i = 0; $i < $results->numRecords(); $i++) {
       $record = $results->nextRecord();
             $states->add(new ArrayData( array('Name' => $record['Name'], 'ID' => $record['ID'], 'HasSpecial' => $record['HasSpecial']) ));
          }
          
          return $states;
       }

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